Python | Split nested list into two lists
Given a nested 2D list, the task is to split the nested list into two lists such that first list contains first elements of each sublists and second list contains second element of each sublists.
Method #1: Using map, zip()
# Python code to demonstrate # splitting nested list # into 2 lists # initialising nested lists ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]] # printing initial lists print ("initial list", str(ini_list)) # code to split it into 2 lists res1, res2 = map(list, zip(*ini_list)) # printing result print("final lists", res1, "\n", res2) |
chevron_right
filter_none
Output:
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Method #2: Using list comprehension
# python code to demonstrate # splitting nested list # into 2 lists # initialising nested lists ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]] # printing initial lists print ("initial list", str(ini_list)) # code to split it into 2 lists res1 = [i[1] for i in ini_list] res2 = [i[0] for i in ini_list] # printing result print("final lists", str(res1), "\n", str(res2)) |
chevron_right
filter_none
Output:
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [2, 3, 65, 2] [1, 4, 45, 223]
Method #3: Using operator.itemgetter()
# python code to demonstrate # splitting nested list # into 2 lists from operator import itemgetter # initialising nested lists ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]] # printing initial lists print ("initial list", str(ini_list)) # code to split it into 2 lists res1 = list(map(itemgetter(0), ini_list)) res2 = list(map(itemgetter(1), ini_list)) # printing result print("final lists", str(res1), "\n", str(res2)) |
chevron_right
filter_none
Output:
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Recommended Posts:
- Python | Split list into lists by particular value
- Python | Split dictionary of lists to list of dictionaries
- Python | Check if a nested list is a subset of another nested list
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Merging nested lists
- Python | Split the Even and Odd elements into two different lists
- Python | Find maximum length sub-list in a nested list
- Python | Pair and combine nested list to tuple list
- Python | Convert a nested list into a flat list
- Python | Split dictionary keys and values into separate lists
- Python | Convert given list into nested list
- Python | Program to count number of lists in a list of lists
- Python | Intersection of two nested list
- Nested List Comprehensions in Python
- Python | How to copy a nested list
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.



