How to make list of lists from list of tuple by position ie
list_of_tuples = [(a, 2), (b, 5), (c, 6)]
to
list_of_lists = [[a, b, c], [2, 5, 6]]
You can use zip, and then map:
list_of_tuples = [(a, 2), (b, 5), (c, 6)]
new_list = zip(*list_of_tuples)
final_list = map(list, new_list)
print final_list
Edit:
As @PM 2Ring pointed out, in python 3, map returns an iterator, so it needs to be passed to list, if you really want a list:
final_list = list(map(list, new_list))
In Python 3+, map function returns map object. Therefore you have to convert map object to list by using: list(map_object).
Here all code:
list_of_tuples = [('a', 2), ('b', 5), ('c', 6)]
zipped_list = zip(*list_of_tuples)
list_of_lists = list(map(list, zipped_list))
print(list_of_lists)
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.