Python | Convert list of tuples to list of list (original) (raw)
Last Updated : 27 Dec, 2024
Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.
Using numpy
NumPy makes it easy to convert a list of tuples into a list of lists in Python. This conversion is efficient and quick, especially when working with large datasets.
Example:
Python `
import numpy as np a= [(1, 2), (3, 4), (5, 6)]
Convert to numpy array and back to list
res = np.array(a).tolist() print(res)
`
Output
[[1, 2], [3, 4], [5, 6]]
**Explanation:
- Converts list of tuples to **a list of lists using NumPy.
- Prints the converted list.
Let's understand different method to convert list of tuples to list of list.
Table of Content
Using List Comprehension
List comprehension offers concise and efficient way to convert list of tuples into list of lists in Python. This method is ideal for small to medium-sized datasets due to its readability and speed.
**Example:
Python `
a= [(1, 2), (3, 4), (5, 6)]
Convert tuples to lists
res = [list(i) for i in a] print(res)
`
Output
[[1, 2], [3, 4], [5, 6]]
**Explanation:
- This convert each tuple in the list
a
into list. - Prints the resulting list of lists after the conversion.
Using map()
map() applies the list() function to each tuple in list, converting them into lists efficiently. This method is ideal for transforming a list of tuples into list of lists in Python.
**Example:
Python `
a= [(1, 2), (3, 4), (5, 6)]
Convert each tuple in the list to a list
res = list(map(list, a)) print(res)
`
Output
[[1, 2], [3, 4], [5, 6]]
**Explanation:
This
applieslist()
to each tuple in the list_**a
**_, converting them into lists.- Result is then printed as list of lists.
itertools.starmap() is an alternative to map(), applying a function to each tuple in the list with argument unpacking. It's an effective but slightly less efficient method to convert list of tuples into list of lists.
Python `
import itertools a = [(1, 2), (3, 4), (5, 6)]
Convert each tuple to a list using starmap
res = list(itertools.starmap(lambda *x: list(x), a)) print(res)
`
Output
[[1, 2], [3, 4], [5, 6]]
**Explanation:
- This converts each tuple to list using
lambda
function. - The resulting list of lists is printed.
Using for
Loop
Using for loop is a simple method to convert list of tuples into list of lists. However, it is less efficient for large datasets due to explicit iteration and repeated append operations.
**Example:
Python `
a= [(1, 2), (3, 4), (5, 6)] res = []
Iterate through each tuple in the list 'a'
for i in a: # Convert each tuple to a list and append it to res res.append(list(i)) print(res)
`
Output
[[1, 2], [3, 4], [5, 6]]
**Explanation:
- Loop converts each tuple in a to a list.
- result is printed as a list of lists.
Similar Reads
- Python - Convert List of Lists to Tuple of Tuples Sometimes, while working with Python data, we can have a problem in which we need to perform interconversion of data types. This kind of problem can occur in domains in which we need to get data in particular formats such as Machine Learning. Let us discuss certain ways in which this task can be per 8 min read
- Convert Set of Tuples to a List of Lists in Python 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 3 min read
- Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a 3 min read
- Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con 5 min read
- Python - Convert List of lists to list of Sets We are given a list containing lists, and our task is to convert each sublist into a set. This helps remove duplicate elements within each sublist. For example, if we have:a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]The output should be:[{1, 2}, {1, 2, 3}, {2}, {0}]Let's explore some method's to ac 2 min read
- Convert List of Tuples to List of Strings - Python The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings.For example, giv 3 min read
- Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co 5 min read
- Python | Convert List of lists to list of Strings Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + joi 4 min read
- Convert List of Tuples To Multiple Lists in Python When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi 3 min read
- Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con 3 min read