I want to convert
l = [(1, 'a'), (2, 'b')]
to
r = [1, 2]
p = ['a', 'b']
Obviously, looping can be used. is it possible to use lambda? Any other ways
Thanks David
Use zip
with *
:
>>> l = [(1, 'a'), (2, 'b')]
>>> r, p = zip(*l)
>>> r
(1, 2)
>>> p
('a', 'b')
You have to use list conprehension and create new variable.
>>> a = [x[0] for x in l]
>>> a
[1, 2]
>>> p = [x[1] for x in l]
>>> p
['a', 'b']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.