Python | Convert Triple nesting to Double nesting list
Last Updated : 17 Apr, 2023
Sometimes, while working with lists, we can have a problem in which we need to perform the flattening of a nested list. This kind of problem has been discussed many times. But sometimes, flattening can be from a triple to double nesting as well. Let's discuss certain ways in which this task can be performed.
Method #1: Using list comprehension
This task can be performed using the technique of list comprehension. In this, one can just take the initial element of the triple nested list and just unpack it to a double nested list.
Python3 # Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # using list comprehension # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list # using list comprehension res = [sub[0] for sub in test_list] # printing result print("Double nested list from triple nested : " + str(res))
Output : The original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Time complexity: O(n), where n is the total number of elements in the list.
Auxiliary space: O(n), where n is the total number of elements in the list.
Method #2: Using chain.from_iterable()
This task can also be performed using this function. This is the inbuilt method that is made to perform the task of flattening a list and hence is highly recommended to perform this task.
Python3 # Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # using chain.from_iterable() from itertools import chain # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list # using list comprehension res = list(chain.from_iterable(test_list)) # printing result print("Double nested list from triple nested : " + str(res))
Output : The original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using extend() method
Python3 # Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list res = [] for i in test_list: x = [] for j in i: x.extend(j) res.append(x) # printing result print("Double nested list from triple nested : " + str(res))
OutputThe original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Method #4: Using numpy.array
Note: Install numpy using "pip install numpy"
The numpy library can be used to convert the triple nested list to a double nested list. Time complexity of this method is O(n) and space complexity is O(n) as well, since we are using numpy to convert the nested list to a flat array.
Python3 # Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # using numpy # importing numpy import numpy as np # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list # using numpy res = np.array(test_list).ravel().tolist() # printing result print("Double nested list from triple nested : " + str(res)) # This code is contributed by Edula Vinay Kumar Reddy
Output:
The original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(n), where n is the total number of elements in the input list.
Method #5 : Using sum() and [] operator
Python3 test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) res = sum(test_list, []) # printing result print("Double nested list from triple nested : " + str(res)) #This code is contributed by Jyothi pinjala
OutputThe original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using map() function and lambda expression
Approach:
- Initialize the list test_list with the given values.
- Print the original list using the print() function and str() function to convert it to a string.
- Use the map() function to apply a lambda expression to each element in the list. The lambda expression takes an element x and returns the first sub-element of x, i.e., x[0].
- Convert the result to a list using the list() function.
- Print the result using the print() function and str() function to convert it to a string.
Python3 # Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # using map() and lambda expression # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list # using map() and lambda expression res = list(map(lambda x: x[0], test_list)) # printing result print("Double nested list from triple nested : " + str(res))
OutputThe original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), where n is the number of elements in the list (for storing the result list).
Similar Reads
Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read
Convert List of Tuples To Multiple Lists in Python When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi
3 min read
Python | Pair and combine nested list to tuple list Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the
10 min read
Convert List to Tuple in Python The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read
Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read