Given a nested 2D list, the task is to split the nested list into two lists such that the first list contains the first elements of each sublist and the second list contains the second element of each sublist. In this article, we will see how to split nested lists into two lists in Python.
Python Split Nested List into Two Lists
Below are the ways by which we can split nested lists into two lists in Python:
- Using map and zip()
- Using list comprehension
- Using operator.itemgetter()
- Using extend() method
- Using itertools
- Using numpy.transpose()
Split Nested List into Two Lists with map and zip()
In this example, the Python code starts with the nested list ini_list
and splits it into two separate lists, res1
and res2
, using the zip
function along with map
and list
conversions. The output displays the initial nested list and the two resulting 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)
|
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Time complexity: O(n)
Auxiliary Space: O(n)
Python Split Nested List into Two Lists Using List Comprehension
In this example, the Python code initializes a nested list ini_list
and then splits it into two separate lists, res1
and res2
, by extracting the second and first elements of each inner list, respectively. The output displays the initial nested list and the two resulting 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))
|
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [2, 3, 65, 2] [1, 4, 45, 223]
Time Complexity: O(n)
Auxiliary Space: O(n)
Split Nested List into Two Lists with operator.itemgetter()
In this example, the Python code initializes a nested list ini_list
and splits it into two separate lists, res1
and res2
, using the itemgetter
function from the operator
module to extract the first and second elements of each inner list, respectively.
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))
|
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Time complexity: O(n)
Auxiliary Space: O(n)
Python Split Nested List into Two Lists Using extend() Method
In this example, the Python code initializes a nested list ini_list
and splits it into two separate lists, res1
and res2
, by flattening the inner lists and then alternately distributing their elements into the two resulting lists based on their index positions.
# initialising nested lists ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
# printing initial lists print ( "initial list" , str (ini_list))
x = []
for i in ini_list:
x.extend(i)
res1 = []
res2 = []
for i in range ( 0 , len (x)):
if (i % 2 = = 0 ):
res1.append(x[i])
else :
res2.append(x[i])
# printing result print ( "final lists" , str (res1), "\n" , str (res2))
|
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Time Complexity: O(n), where n is length of x list.
Auxiliary Space: O(n+m), where n is length of res1 list and m is length of res2 list.
Split Nested List into Two Lists Using itertools
In this example, the Python code initializes a nested list ini_list
and splits it into two separate lists, res1
and res2
, using the itertools.zip_longest
function to pair elements from the inner lists. The output displays the initial nested list and the two resulting lists, where missing values are filled with an empty string.
import itertools
# 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 = list (itertools.zip_longest( * ini_list, fillvalue = ''))
res1, res2 = list (res1), list (res2)
# printing result print ( "final lists" , str (res1), "\n" , str (res2))
# This code is contributed by Edula Vinay Kumar Reddy |
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Time complexity: O(n)
Auxiliary Space: O(n)
Python Split Nested List into Two Lists Using numpy.transpose()
In this example, the Python code initializes a nested list ini_list
and converts it into a NumPy array (np_arr
). It then transposes the NumPy array (tr_np_arr
) and converts it back to two separate lists, res1
and res2
.
# importing required module import numpy as np
# initialising nested list ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
# printing initial list print ( "Initial List: " , ini_list)
# Converting nested list to numpy array np_arr = np.array(ini_list)
# Transposing numpy array tr_np_arr = np.transpose(np_arr)
# Converting transposed numpy array to list res1, res2 = tr_np_arr.tolist()
# printing result print ( "Final lists: " , res1, "\n" , res2)
|
Output:
Initial List: [[1, 2], [4, 3], [45, 65], [223, 2]]
Final lists: [1, 4, 45, 223]
[2, 3, 65, 2]
Time complexity: O(n)
Auxiliary Space: O(n)