Skip to content Skip to sidebar Skip to footer

Adding Item To Dictionary Python With A Variable Name Key

I made a list of lists of possible combinations of n length from a list of items, and now I want to create a dictionary where each key is one of the items from list of lists of pos

Solution 1:

You can use tuples as keys:

mydict = { (1, 2, 4): 0 }

If you want to count things, take a look at collections.Counter, it makes counting trivial, no need to initialize the keys to 0:

counts = collections.Counter()
counts[(1, 2, 4)] += 1

Post a Comment for "Adding Item To Dictionary Python With A Variable Name Key"