wasimbhalli wasimbhalli - 1 year ago 110
Python Question

How to get first element in a list of tuples?

I have a list like below where the first element is the id and the other is a string:

[(1, u'abc'), (2, u'def')]

I want to create a list of ids only from this list of tuples as below:


I'll use this list in
so it needs to be a list of integer values. Please help!

Answer Source
>>>a = [(1, u'abc'), (2, u'def')]
>>>b = [int(i[0]) for i in a]
[1, 2]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download