Bardolf
Bardolf

Reputation: 17

Making a dict of a list with more then 1 value

I am a rookie at python, having a hard time figuering out how lists and dict really works. In my program i have a list that looks like:

Hat =[334,hat,59,200]

that i want to make into a dict, with a key 334 and the vaule = [hat,59,200]. How could i make it so?

Upvotes: 0

Views: 72

Answers (1)

phihag
phihag

Reputation: 288070

Simply extract the first and all further elements with a slice:

{Hat[0]: Hat[1:]}

If you had multiple hats, you can use a dictionary comprehension:

hats = [
    [334,'hat',59,200],
    [123,'chapeau',19,300],
    [999,'hut',1,100],
]

print( {Hat[0]: Hat[1:] for Hat in hats} )

Upvotes: 3

Related Questions