Tuesday, April 24, 2012

Best way to go from tuple pairs of numbers to one tuple of all the individual numbers

How can i get this



nums = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)]


to this?



[0, 1, 2, 3, 4, 5, 6, 7, 8]


I did:



>>> zip(*nums)[0]
(0, 1, 2, 3, 4, 5, 6, 7)


But it gives me everything except the last element and then i had to use some bad code to get it to the correct result so i was looking for an elegant solution.





No comments:

Post a Comment