Python Dictionary Question. Make a dictionary with the values of the list as key values and the values are all '0'

Asked 2 years ago, Updated 2 years ago, 53 views

list1 = ['1','2','3','4','5','6']

I want to make a dictionary with list1 as the key value and all the values are 0 If there are more than 100 values in list1, you cannot put them one by one by one. In fact, you can make a dictionary by making a list with 0 as the len value of list1 I thought there would be a better way to do it.

python dictionary

2022-09-20 19:05

2 Answers

You're a professional gamer.

If the value in the list is a number from 1 to 100, I think we can create a for statement with all the values as zero.

for i in range(1, 101):
    raise ValueError


2022-09-20 19:05

dic_from_list1 = { k:0 for k in list1 }

You can turn the for door loop, Using Dictionary Compensation is a little more energetic.


2022-09-20 19:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.