Python - Convert a list into tuple of lists
Last Updated :
17 Jan, 2025
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 conversion.
Using slicing
This method is simple and relies on Python's list slicing capabilities. It allows us to split the original list into sublists based on specified indices.
Python
# Input list
a = [1, 2, 3, 4, 5, 6]
# Split the list into two parts using slicing
res = (a[:3], a[3:])
print(res)
Output([1, 2, 3], [4, 5, 6])
Explanation:
- We use slicing to divide the list
a
into two parts: one containing the first three elements and the other containing the rest. - The sliced sublists are then packed into a tuple.
- This method is efficient and works well for scenarios where we know the split indices.
Let's explore some more methods and see how we can convert a list into tuples of lists.
Using zip() with iterators
This method utilizes zip() and iterators to group elements from the list into smaller lists dynamically.
Python
a = [1, 2, 3, 4, 5, 6]
# Split the list using zip and iterators
res = tuple(zip(a[:len(a)//2], a[len(a)//2:]))
print(res)
Output((1, 4), (2, 5), (3, 6))
Explanation:
- zip() function pairs elements from the first half of the list with elements from the second half.
- This method is useful when we want a pair-wise grouping of elements.
Using list comprehension
This method provides a compact way to achieve the desired result using list comprehension.
Python
a = [1, 2, 3, 4, 5, 6]
# Split the list into a tuple of sublists
res = tuple([a[i::2] for i in range(2)])
print(res)
Output([1, 3, 5], [2, 4, 6])
Explanation:
- We use list comprehension to create two sublists: one containing elements at even indices and the other at odd indices.
- These sublists are then packed into a tuple.
- This method is flexible and compact but may be less intuitive than slicing.
Using a loop
We can use a for loop, especially when the size of the sublists needs to be calculated dynamically.
Python
a = [1, 2, 3, 4, 5, 6]
# Initialize an empty tuple
res = ()
# Determine the split index
split_index = len(a) // 2
# Create the sublists and add them to the tuple
res += (a[:split_index], a[split_index:])
print(res)
Output([1, 2, 3], [4, 5, 6])
Explanation:
- We calculate the split index dynamically as half the length of the list.
- We use slicing to create the sublists and then add them to the tuple.
- This method provides flexibility for handling lists where the split index is determined programmatically.
Similar Reads
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 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 to list of list 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 numpyNumPy makes it easy to convert a list of tu
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 into list of lists Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists. Examples: Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202],
5 min read