5 Best Ways to Convert List of Tuples to List of Lists in Python

πŸ’‘ Problem Formulation: When working with data structures in Python, it’s common to encounter a scenario where you have a list of tuples, but the requirements of your program necessitate a list of lists instead. For example, you might start with [(1, 2), (3, 4), (5, 6)] and need to convert it to [[1, 2], [3, 4], [5, 6]]. This article demonstrates five ways to accomplish this conversion efficiently in Python.

Method 1: Using a List Comprehension

This method is Pythonic and concise. List comprehensions are a feature of Python that provide a way to create lists based on existing lists. It follows the form [expression for item in iterable], making it perfect for transforming a list of tuples into a list of lists.

Here’s an example:

input_list = [(1, 2), (3, 4), (5, 6)]
output_list = [list(tup) for tup in input_list]

The output of this code snippet:

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

The provided code uses a list comprehension to iterate over each tuple in the input list, converting each tuple into a list with the list() constructor, and gathers the created lists into a new list.

Method 2: Using the Built-in map function

The map() function applies a given function to each item of an iterable (list, tuple, etc.) and returns a map object. By passing the list constructor as the function and the list of tuples as the iterable, the map object can then be converted back into a list.

Here’s an example:

input_list = [(1, 2), (3, 4), (5, 6)]
output_list = list(map(list, input_list))

The output of this code snippet:

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

This snippet applies the list constructor to each tuple in the input list, creating a map object of lists, which is then converted into a list of lists with the list() function.

Method 3: Using a for Loop

For those who prefer a more traditional approach, using a for loop allows you to iterate through the list of tuples and convert each tuple to a list explicitly. This method offers more control over the process and is easy for beginners to understand.

Here’s an example:

input_list = [(1, 2), (3, 4), (5, 6)]
output_list = []
for tup in input_list:
    output_list.append(list(tup))

The output of this code snippet:

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

In this example, a for loop is used to iterate over each tuple in the input list. The list() function converts each tuple to a list, which is then appended to the output list, resulting in a list of lists.

Method 4: Using Python’s unpacking operator *

This method takes advantage of Python’s unpacking operator (*), which can be quite handy. The operator unpacks the elements from the tuple, which are then passed to the list() constructor. This is particularly useful in complex data transformations.

Here’s an example:

input_list = [(1, 2), (3, 4), (5, 6)]
output_list = [list(*tup) for tup in input_list]

The output of this code snippet:

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

The presented snippet demonstrates a list comprehension where the unpacking operator is applied to each tuple, effectively passing its separate items to the list constructor and creating a new list of lists.

Bonus One-Liner Method 5: Using lambda and map function

Combining a lambda function with the map function allows for a one-liner that is both compact and efficient. The lambda function takes a tuple and converts it directly into a list, which is then passed over the iterable by the map function.

Here’s an example:

input_list = [(1, 2), (3, 4), (5, 6)]
output_list = list(map(lambda x: list(x), input_list))

The output of this code snippet:

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

In this compact form, the lambda function is purely used for converting a tuple to a list and is embedded within the call to map(), encapsulating the conversion in a single expression.

Summary/Discussion

  • Method 1: List Comprehension. Simple and Pythonic. May not be as readable for Python beginners.
  • Method 2: Built-in map function. Concise and functional. Less intuitive for those unfamiliar with functional programming concepts.
  • Method 3: For loop. More control and traditionally readable. Not as succinct as other methods.
  • Method 4: Unpacking operator. Elegant in certain scenarios. May confuse those who don’t know about unpacking.
  • Bonus Method 5: Lambda with map. One-liner that’s compact and elegant. Requires understanding of both map and lambda functions.