Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enumerate and get the first item of each tuple?

How to get from this:

col = [('red', '132', '234'), ('green', '236', '434'), ('brown', '542', '457')]

to this:
EDIT (without any quotes)

1 red, 2 green, 3 brown   # enumerate and get the first item of each tuple.

I tried this, but it doesn’t work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[zip(((enumerate(col),1),i[0])) for i in col]

Only built-in functions please.

>Solution :

You can use list comprehension.

>>> col = [('red', '132', '234'), ('green', '236', '434'), ('brown', '542', '457')]
>>>
>>> ["{} {}".format(index, first) for index, (first, *_) in enumerate(col, start=1)]
['1 red', '2 green', '3 brown']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading