-3

I have a list like ['ASDE','apple','ASWED','orange']

how I can make a dictionary like {"ASDE":"apple","ASWED":"orange"}?

timgeb
  • 76,762
  • 20
  • 123
  • 145

3 Answers3

0

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'}
timgeb
  • 76,762
  • 20
  • 123
  • 145
0

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)
spideyclick
  • 162
  • 1
  • 1
  • 12
0

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'}
niraj
  • 17,498
  • 4
  • 33
  • 48