Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list of tuples of different sizes into a dictionary

I have a list of tuples and want to turn this list into a dictionary. However, the tuples can be larger than 2 and not the same size. I would like the first element of each tuple to be the key and the rest to be stored as a array for the value.

l = [(1,'a','b'),(2,'c'),(3,'d','e','f')]

Just doing d=dict(l) doesn't work (didn't really expect it to). I would like to use a list comprehension along of the lines of d = dict([(k,v) for k,v in arr]) but allow for v of arbitrary size.

like image 590
sutee Avatar asked Dec 07 '22 17:12

sutee


1 Answers

d = dict( (v[0], v[1:]) for v in arr )
like image 155
Winston Ewert Avatar answered May 12 '23 18:05

Winston Ewert