Python | Convert nested sublist into tuples
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 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)]]
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 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)]]
Recommended Posts:
- Python | How to Concatenate tuples to nested tuples
- Python | Find the sublist with maximum value in given nested list
- Python | Convert string tuples to list tuples
- Addition in Nested Tuples - Python
- Python | Unpacking nested tuples
- Python | Convert Tuples to Dictionary
- Python | Convert String to list of tuples
- Python | Convert list of tuples into digits
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list elements to bi-tuples
- Python | Convert Lists to column tuples
- Python | Convert list of tuples to dictionary value lists
- Python | Convert list of nested dictionary into Pandas dataframe
- Python | Convert list of strings to list of tuples
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.



