+ 1
list into dictionary
I am trying to turn a list like this [account1, pass1, account2, pass2] or [account1=pass1, account2=pass2] into a dictionary {account1:pass1, account2:pass2} does anyone know how I can do that in python 3?
1 ответ
+ 3
Here is one of many ways you can do this:
list_1 = [account1, pass1, account2, pass2]
dict = {}
for i in range(0, len(list_1), 2):
dict[list_1[i]] = list_1[i + 1]
The loop will jump by 2 places each time it finishes. It will add the values of list_1[i] as the key, and the list_1[i + 1] as the value of that key. This algorithm assumes that the list given contains both a key and a value.