Python | Split nested list into two lists
Last Updated :
05 Dec, 2023
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:
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.
Python3
ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
print ( "initial list" , str (ini_list))
res1, res2 = map ( list , zip ( * ini_list))
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)
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.
Python3
ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
print ( "initial list" , str (ini_list))
res1 = [i[ 1 ] for i in ini_list]
res2 = [i[ 0 ] for i in ini_list]
print ( "final lists" , str (res1), "\n" , str (res2))
|
Output
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.
Python3
from operator import itemgetter
ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
print ( "initial list" , str (ini_list))
res1 = list ( map (itemgetter( 0 ), ini_list))
res2 = list ( map (itemgetter( 1 ), ini_list))
print ( "final lists" , str (res1), "\n" , str (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)
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.
Python3
ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
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])
print ( "final lists" , str (res1), "\n" , str (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), 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.
Python3
import itertools
ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
print ( "initial list" , str (ini_list))
res1, res2 = list (itertools.zip_longest( * ini_list, fillvalue = ''))
res1, res2 = list (res1), list (res2)
print ( "final lists" , str (res1), "\n" , str (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)
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
.
Python3
import numpy as np
ini_list = [[ 1 , 2 ], [ 4 , 3 ], [ 45 , 65 ], [ 223 , 2 ]]
print ( "Initial List: " , ini_list)
np_arr = np.array(ini_list)
tr_np_arr = np.transpose(np_arr)
res1, res2 = tr_np_arr.tolist()
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)
Please Login to comment...