Single line for-loop to build a dictionary?

39,339

Actually in this specific case you don't even need a dictionary comprehension since you are using duplicate key/value pairs

>>> bigList = [1, 2, 3, 4, 5]
>>> dict(zip(bigList, bigList))
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
Share:
39,339
easythrees
Author by

easythrees

Updated on December 31, 2020

Comments

  • easythrees
    easythrees over 3 years

    I'm constructing a dictionary (which I'll later make into a JSON string). I construct it like this:

    data = {}
    for smallItem in bigList:
        data[smallItem] = smallItem
    

    How can I make that for loop one line?