Sometimes, while working with huge amount of data, we might have in which each point of data constitutes of several elements and needs to be processed as single unit. For this, we might sometimes, just receive a matrix and it’s constituent data points are also lists, which don’t make much sense and whole and might be required to be converted to tuples. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using tuple() + list comprehension This is the one-liner approach to solve this problem. In this we just iterate through the innermost sublist and convert each of lists to tuple using tuple().
Python3
# Python3 code to demonstrate working of # Convert nested sublist into tuples # Using tuple() + list comprehension # Initializing list test_list = [[[ 1 , 2 , 3 ], [ 4 , 6 , 7 ]], [[ 6 , 9 , 8 ], [ 10 , 11 , 12 ]]] # printing original list print ("The original list is : " + str (test_list)) # Convert nested sublist into tuples # Using tuple() + list comprehension res = [[ tuple (ele) for ele in sub] for sub in test_list] # printing result print ("The data after conversion to tuple is : " + str (res)) |
The original list is : [[[1, 2, 3], [4, 6, 7]], [[6, 9, 8], [10, 11, 12]]] The data after conversion to tuple is : [[(1, 2, 3), (4, 6, 7)], [(6, 9, 8), (10, 11, 12)]]
The time complexity of this code is O(n^3) where n is the number of elements in the nested list.
The space complexity of this code is O(n^2) because it creates a new list with tuples for each sublist in the original nested list.
Method #2 : Using map() + list comprehension + tuple This is another way in which this task can be performed. In this, we reduce one inner loop and extend the functionality of tuple conversion to each element using map().
Python3
# Python3 code to demonstrate working of # Convert nested sublist into tuples # Using map() + list comprehension + tuple # Initializing list test_list = [[[ 1 , 2 , 3 ], [ 4 , 6 , 7 ]], [[ 6 , 9 , 8 ], [ 10 , 11 , 12 ]]] # printing original list print ("The original list is : " + str (test_list)) # Convert nested sublist into tuples # Using map() + list comprehension + tuple res = [ list ( map ( tuple , sub)) for sub in test_list] # printing result print ("The data after conversion to tuple is : " + str (res)) |
The original list is : [[[1, 2, 3], [4, 6, 7]], [[6, 9, 8], [10, 11, 12]]] The data after conversion to tuple is : [[(1, 2, 3), (4, 6, 7)], [(6, 9, 8), (10, 11, 12)]]
Method #3 : Using tuple()+recursion
In this method, we define a recursive function convert_to_tuples that takes a list as input. If the input is a list, we use a list comprehension to recursively call the convert_to_tuples function on each sublist of the input, and then return a tuple of the resulting sub-tuples. If the input is not a list, we simply return the input.We then call this function on the original nested list and store the result in tuple_lst, and print both the original list and the converted list.
Python3
# Define a function that takes a nested list as input and converts it to a list of tuples def convert_to_tuples(nested_lst): # Create an empty list to store the result tuple_lst = [] # Loop through each sublist in the nested list for sublist in nested_lst: # Convert the sublist to a tuple and append it to the result list tuple_lst.append( tuple (sublist)) # Return the resulting list of tuples return tuple_lst # Define the original nested list nested_lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] # Call the conversion function and store the result in a new variable tuple_lst = convert_to_tuples(nested_lst) # Print the original and converted lists for comparison print ( "Original nested list:" , nested_lst) print ( "Converted list of tuples:" , tuple_lst) |
Original nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Converted list of tuples: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using the reduce() function :
Step-by-step approach:
- Create an empty list to store the result.
- Loop through each sublist in the nested list.
- Convert the sublist to a tuple.
- Append the tuple to the result list.
- Return the resulting list of tuples.
Below is the implementation of the above approach:
Python3
from functools import reduce # Define the original nested list nested_lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] # Use reduce function to flatten the nested list flat_lst = reduce ( lambda x,y: x + y, nested_lst) # Use a list comprehension to create tuples from the flat list tuple_lst = [ tuple (flat_lst[i:i + len (nested_lst)]) for i in range ( 0 , len (flat_lst), len (nested_lst))] # Print the original and converted lists for comparison print ( "Original nested list:" , nested_lst) print ( "Converted list of tuples:" , tuple_lst) #This code is contributed by Jyothi pinjala |
Original nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Converted list of tuples: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
The time complexity : O(n^2), where n is the length of the longest sublist. This is because the algorithm needs to loop through each sublist and convert it to a tuple. Since each sublist has length n, the time complexity of converting a sublist to a tuple is O(n). Therefore, the overall time complexity is O(n^2).
The space complexity : O(n), where n is the total number of elements in the nested list. This is because the algorithm needs to create a list of tuples with the same number of elements as the original nested list. Since each element in the nested list is represented by a single value in the tuple, the space complexity of the resulting list of tuples is the same as the number of elements in the nested list, which is O(n). The additional space used by the algorithm is negligible (i.e. constant), since it only creates a single empty list and a single variable to store each sublist temporarily.
Method 5: Using nested loops
Step-by-step approach:
- Define the original nested list
- Create an empty list to store the flattened elements
- Use nested loops to iterate over each element in the nested list
- Append each element to the flattened list
- Create an empty list to store the tuples
- Use a for loop and the range function to iterate over the flattened list in chunks of the length of the original nested list
- Use the tuple function to convert each chunk into a tuple
- Append each tuple to the tuple list
- Print the original and converted lists for comparison
Below is the implementation of the above approach:
Python3
# Define the original nested list nested_lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] # Flatten the nested list using nested loops flat_lst = [] for sublist in nested_lst: for element in sublist: flat_lst.append(element) # Convert the flat list into a list of tuples tuple_lst = [] for i in range ( 0 , len (flat_lst), len (nested_lst)): tuple_lst.append( tuple (flat_lst[i:i + len (nested_lst)])) # Print the original and converted lists for comparison print ( "Original nested list:" , nested_lst) print ( "Converted list of tuples:" , tuple_lst) |
Original nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Converted list of tuples: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
Time complexity: O(n^2), where n is the total number of elements in the nested list.
Auxiliary space: O(n), where n is the total number of elements in the nested list.
Method #6: Using itertools.chain() and zip() function
The itertools module provides a powerful function called chain() that can be used to flatten the nested list. The zip() function is used to convert the flat list into a list of tuples.
Step-by-step approach:
- Import the itertools module.
- Define the original nested list.
- Use the chain() function to flatten the nested list.
- Use the zip() function to convert the flat list into a list of tuples.
- Print the original and converted lists for comparison.
Python3
import itertools # Define the original nested list nested_lst = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] # Flatten the nested list using itertools.chain() flat_lst = list (itertools.chain( * nested_lst)) # Convert the flat list into a list of tuples using zip() tuple_lst = list ( zip ( * [ iter (flat_lst)] * len (nested_lst))) # Print the original and converted lists for comparison print ( "Original nested list:" , nested_lst) print ( "Converted list of tuples:" , tuple_lst) |
Original nested list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Converted list of tuples: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
The time complexity of the code is O(N), where N is the total number of elements in the nested list. T
The auxiliary space is also O(N), as it creates two new lists that each contain all the elements from the nested list.