# 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)) `

Output :

```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)) `

Output :

```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)]]
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.