Python | Convert List of Lists to Tuple of Tuples

In this program, we have a list of lists. We need to create a Python program that will convert list of lists to tuple of tuples.
Submitted by Shivang Yadav, on October 31, 2021

Sometimes programmers have data in one type of collection and we need to convert it into another type of collection. Here, we will learn to convert nested collections from one type to another. Here, we will see a Python program to convert a list of lists to the tuple of tuples.

Input :
[[4, 1, 9], [2, 6, 8], [5, 9, 1]]

Output:
((4, 1, 9), (2, 6, 8), (5, 9, 1))

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Tuple to tuples is a nested collection in which a tuple consists of tuples as elements.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

List of Lists is a nested collection in which a list consists of lists as elements.

Converting List of Lists to Tuple of Tuples

We have a list of lists and we will be converting it to a tuple of tuples.

For this, we will be taking each tuple from the tuple to tuples and converting it into a tuple. And then convert the whole encapsulating list to a tuple. In python, we can perform this task using multiple ways.

Method 1:

One way to solve the problem is by converting each list of the list of list to tuple individually using tuple() method. And then converting the main container. This is done using list comprehension along with the tuple() method.

# Python program to Convert list of lists 
# to Tuple of Tuples 

# Initializing and printing List of lists 
listOflists = [[4, 1, 9], [2, 6, 8], [5, 9, 1]]
print("Initial List of Lists : " + str(listOflists))
  
# Converting List of Lists to Tuple of Tuples
convTupleOfTuple = tuple(tuple(data) for data in listOflists)
  
# Printing the Tuple of Tuples
print("Converted Tuple of Tuples : " + str(convTupleOfTuple))

Output:

Initial List of Lists : [[4, 1, 9], [2, 6, 8], [5, 9, 1]]
Converted Tuple of Tuples : ((4, 1, 9), (2, 6, 8), (5, 9, 1))

Method 2:

One method that can solve the problem is by using map() to map the converted individual list which will be converted using tuple() method.

# Python program to Convert list of lists 
# to Tuple of Tuples 

# Initializing and printing List of lists 
listOflists = [[4, 1, 9], [2, 6, 8], [5, 9, 1]]
print("Initial List of Lists : " + str(listOflists))
  
# Converting List of Lists to Tuple of Tuples
convTupleOfTuple = tuple(map(tuple, listOflists))
  
# Printing the Tuple of Tuples
print("Converted Tuple of Tuples : " + str(convTupleOfTuple))

Output:

Initial List of Lists : [[4, 1, 9], [2, 6, 8], [5, 9, 1]]
Converted Tuple of Tuples : ((4, 1, 9), (2, 6, 8), (5, 9, 1))

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.