Python | Group elements at same indices in a multi-list
Flattening a 2D list to one is a common problem that is faced in many domains. But sometimes we require to pair elements at specific indices as one, so that elements at respective indices are together. This problem is not common but still having a solution to it helps.
Let’s discuss certain ways to pair elements at specific indices.
Method #1 : Using list comprehension + zip()
List comprehension can be used to achieve this particular task along with zip function which does the task of pairing the like indices together. This method is just shorthand to the naive method.
# Python3 code to demonstrate # index list elements pairing # using list comprehension # initializing list test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]] # printing original list print ("The original list is : " + str(test_list)) # using list comprehension # to perform index list elements pairing res = [list(x) for x in zip(*test_list)] # printing result print ("The index elements pairs list is " + str(res)) |
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] The index elements pairs list is [[1, 4, 8], [4, 6, 3], [5, 8, 10]]
Method #2 : Using map() + zip()
map function can be used to map each iteration result into a single list while zip function performs the index element pairing. This combination can be used to achieve the desired result.
# Python3 code to demonstrate # index list elements pairing # using map() + zip() # initializing list test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]] # printing original list print ("The original list is : " + str(test_list)) # using map() + zip() # to perform index list elements pairing res = list(map(list, zip(*test_list))) # printing result print ("The index elements pairs list is " + str(res)) |
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] The index elements pairs list is [[1, 4, 8], [4, 6, 3], [5, 8, 10]]
Method #3 : Using zip()
Using zip function alone does it all and just needs to be typecast to list to print result in list format. It’s the most pythonic way to perform this task and the most elegant as well.
# Python3 code to demonstrate # index list elements pairing # using zip() # initializing list test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]] # printing original list print ("The original list is : " + str(test_list)) # using zip() # to perform index list elements pairing res = list(zip(*test_list)) # printing result print ("The index elements pairs list is " + str(res)) |
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] The index elements pairs list is [(1, 4, 8), (4, 6, 3), (5, 8, 10)]
Recommended Posts:
- Python | Indices of N largest elements in list
- Python | Find elements of a list by indices
- Python | Group elements on break positions in list
- Python | Group consecutive list elements with tolerance
- Python | Group list elements based on frequency
- Python | Indices of sorted list of list elements
- Python | Get match indices
- Python | Indices of numbers greater than K
- Python | Find indices with None values in given list
- Python | Duplicate element indices in list
- Python | Ways to find indices of value in list
- Python | Get indices of True values in a binary list
- Python program to fetch the indices of true values in a Boolean list
- Python | Group Anagrams from given list
- Python | Group tuples in list with same first value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



