Open In App

Convert Set of Tuples to a List of Lists in Python

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list. You will be guided through the ideas and procedures required for this conversion by this guide. In this article, we will see how to convert set of tuples to a list of lists in Python.

Convert a Set of Tuples to a List of Lists

Below are some of the ways by which we can convert a set of tuples to a list of lists in Python:

  • Using list comprehension
  • Using map() Function
  • Using Nested Loops
  • Using a Function

Convert Set of Tuples to a List of Lists Using List Comprehension

Using list comprehension is one of the most direct approaches. This method creates lists from each tuple by iterating over the collection of tuples. In this example, the list comprehension loops over each tuple in the set (tuple_set) and uses the list(t) expression to turn each tuple into a list.

Python3




# Input set of tuples
tuple_set = {(1, 2), (3, 4), (5, 6)}
  
# Convert set of tuples to list of lists using list comprehension
list_of_lists = [list(t) for t in tuple_set]
  
# Output
print(list_of_lists)


Output

[[1, 2], [3, 4], [5, 6]]

Python Set of Tuples to a List of Lists Using map() Function

The map() method takes an input iterable and applies a given function to every item in the iterable. In this scenario, the list() constructor may be used with map(). Each tuple in the set is given the list() constructor by the code map(list, tuple_set), and list() subsequently turns the map object into a list.

Python3




# Input set of tuples
tuple_set = {(1, 2), (3, 4), (5, 6)}
  
# Convert set of tuples to list of lists using map() function
list_of_lists = list(map(list, tuple_set))
  
# Output
print(list_of_lists)


Output

[[1, 2], [3, 4], [5, 6]]

Set of Tuples to a List of Lists in Python Using Nested Loops

Using nested loops to traverse over the tuples and their components is a more detailed method. Here, each tuple in the set (tuple_set) is iterated over by the outer loop, and list(inner_tuple) is used by the inner loop to turn each tuple into a list.

Python3




# Input set of tuples
tuple_set = {(1, 2), (3, 4), (5, 6)}
  
# Convert set of tuples to list of lists using nested loops
list_of_lists = []
for t in tuple_set:
    list_of_lists.append(list(t))
  
# Output
print(list_of_lists)


Output

[[1, 2], [3, 4], [5, 6]]

Set of Tuples to a List of Lists Using a Function

You may write a function that accepts a collection of tuples as input and outputs a list of lists if you want a more modular approach.

Python3




# Function to convert a set of tuples to a list of lists
def convert_to_list_of_lists(tuple_set):
    return [list(t) for t in tuple_set]
  
# Input set of tuples
tuple_set = {(1, 2), (3, 4), (5, 6)}
  
# Convert set of tuples to list of lists using a function
list_of_lists = convert_to_list_of_lists(tuple_set)
  
# Output
print(list_of_lists)


Output

[[1, 2], [3, 4], [5, 6]]



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads