I have a list like ['ASDE','apple','ASWED','orange']
how I can make a dictionary like
{"ASDE":"apple","ASWED":"orange"}
?
I have a list like ['ASDE','apple','ASWED','orange']
how I can make a dictionary like
{"ASDE":"apple","ASWED":"orange"}
?
Here's an answer. Since you did not really put much effort into your question, I won't bother explaining.
>>> l = ['ASDE','apple','ASWED','orange']
>>> dict(zip(*[iter(l)]*2))
{'ASWED': 'orange', 'ASDE': 'apple'}
Assuming you're using Python, here's a version that uses a for loop:
myList=['ASDE', 'apple', 'ASWED', 'orange']
myDictionary={}
counter=0
for item in myList:
if counter %2 == 0:
myDictionary[item]='new'
indexItem=item
else:
myDictionary[indexItem]=item
counter+=1
print(myDictionary)
One possible way is we can iterate through list with step of 2 instead of 1 and store items as key and value:
my_dict = {my_list[index]: my_list[index+1] for index in range(0, len(my_list), 2)}
my_dict
Output:
{'ASDE': 'apple', 'ASWED': 'orange'}