You are currently viewing Convert List of Lists to Tuple of Tuples in Python

How to convert a list of lists to a tuple of tuples in python? A list of lists is often used to group related values together, and converting it to a tuple of tuples can be useful for further data processing or analysis. A list is a collection of items with the same types whereas Tuples can have multiple data types.

Advertisements

You can convert a list of lists to a tuple of tuples in python using many ways, for example, by using tuple()+list comprehension, enumerate(), map()+tuple(), and recursive functions. In this article, I will explain convert a list of lists to a tuple of tuples in python by using all these methods with examples.

1. Quick Examples of Convert List of Lists to Tuple of Tuples

If you are in a hurry, below are some quick examples of how to convert a list of lists to a tuple of tuples.


# Quick examples of convert list of lists to tuple of tuples

# Example 1: Convert a list of lists to a tuple of tuples 
# Using tuple() + list comprehension
lists = [[2500, 'Hadoop'], [2200, 'Spark'], [3000, 'Python']] 
result = tuple(tuple(sublist) for sublist in lists)

# Example 2: Using enumerate() function
lists = [[5, 12, 23], [14, 25, 6], [11, 28, 39]]
result = tuple(tuple(i) for x,i in enumerate(lists))

# Example 3: Using map() + tuple() function
result = tuple(map(tuple, lists))

# Example 4: Using the recursive method
def list_to_tuple(List):
    if len(List) == 0:
        return ()
    else:
        return (tuple(List[0]),) + list_to_tuple(List[1:])
# initializing list
lists = [[15, 8, 26], [16, 12, 4], [13, 48, 24]]
result = list_to_tuple(lists)

2. Convert List of Lists to Tuple of Tuples Using tuple() + List Comprehension

You can convert a list of lists to a tuple of tuples in Python, you can use a list comprehension and the built-in tuple() function to convert each sublist to a tuple. For example, you can use a list comprehension to iterate over each sublist in the lists, and then apply the tuple() function to each sublist to convert it to a tuple.


# Initializing list
lists = [[2500, 'Hadoop'], [2200, 'Spark'], [3000, 'Python']]
print("Orginal list of lists: ",lists)
 
# Convert list of lists to tuple of tuples 
# Using tuple() + list comprehension 
result = tuple(tuple(sublist) for sublist in lists)
print("Updated tuple of tuple: ",result)

# Output:
# Orginal list of lists:  [[2500, 'Hadoop'], [2200, 'Spark'], [3000, 'Python']]
# Updated tuple of tuple:  ((2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python'))

3. Using enumerate() Function

You can also use the enumerate() function to convert a list of lists to a tuple of tuples. For example, you first define a list of lists called lists, use the list comprehension to iterate over each sublist lists. then use the enumerate() function to get the index and value of each sublist in lists. For each sublist, you convert list to a tuple using the tuple() function.


# initializing list
lists = [[5, 12, 23], [14, 25, 6], [11, 28, 39]]
print("Orginal list of lists: ",lists)
 
# Convert list of lists to tuple of tuples 
# Using enumerate() function
result = tuple(tuple(i) for x,i in enumerate(lists))
print("Updated tuple of tuple: ",result)

# Output:
# Orginal list of lists:  [[5, 12, 23], [14, 25, 6], [11, 28, 39]]
# Updated tuple of tuple:  ((5, 12, 23), (14, 25, 6), (11, 28, 39))

4. Using map() + tuple()

You can also use the map() function along with tuple() to convert. For example, you first define a list of lists called lists. You can use the map() function to apply the tuple() function to each sublist in lists. The map() function returns a map object, which you convert to a tuple using the tuple() function.


# Initializing list
lists = [[12, 24, 16], [15, 8, 19], [5, 18, 25]]
print("Orginal list of lists: ",lists)
 
# Convert list of lists to tuple of tuples 
# Using map() + tuple() function
result = tuple(map(tuple, lists))
print("Updated tuple of tuple: ",result)

# Output
# Orginal list of lists:  [[12, 24, 16], [15, 8, 19], [5, 18, 25]]
# Updated tuple of tuple:  ((12, 24, 16), (15, 8, 19), (5, 18, 25))

5. Using the Recursive Method

Similarly, you can also convert a list of lists to a tuple of tuples using a recursive method, you can define a recursive function that takes in a list as input and iterates through its elements.


# Using recursive method
def list_to_tuple(List):
    if len(List) == 0:
        return ()
    else:
        return (tuple(List[0]),) + list_to_tuple(List[1:])
# initializing list
lists = [[15, 8, 26], [16, 12, 4], [13, 48, 24]]
print("Orginal list of lists: ",lists)
 
result = list_to_tuple(lists)
print("Updated tuple of tuple: ",result)

# Output
# Orginal list of lists:  [[15, 8, 26], [16, 12, 4], [13, 48, 24]]
# Updated tuple of tuple:  ((15, 8, 26), (16, 12, 4), (13, 48, 24))

Conclusion

In this article, I have explained how to convert a list of lists to a tuple of tuples in python by using tuple()+list comprehension, enumerate(), map() + tuple(), and recursive functions with examples.

Happy Learning !!

Related Articles

References