Python | Convert list to indexed tuple list
Last Updated :
12 Apr, 2023
Sometimes, while working with Python lists, we can have a problem in which we need to convert a list to tuple. This kind of problem have been dealt with before. But sometimes, we have it's variation in which we need to assign the index of the element along with element as a tuple. Let's discuss certain ways in which this task can be performed.
Method #1 : Using list() + enumerate()
Combination of above functions can be used to perform this particular task. In this, we just pass the enumerated list to list(), which returns the tuple with first element as index and second as list element at that index.
Python3
# Python3 code to demonstrate working of
# Convert list to indexed tuple
# Using list() + enumerate()
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Convert list to indexed tuple
# Using list() + enumerate()
res = list(enumerate(test_list))
# Printing result
print("List after conversion to tuple list : " + str(res))
Output :
The original list : [4, 5, 8, 9, 10]
List after conversion to tuple list : [(0, 4), (1, 5), (2, 8), (3, 9), (4, 10)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. list() + enumerate() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2: Using zip() + range()
The combination of above functions can also be used to perform this particular task. In this, we just use the ability of zip() to convert to tuple and range() to get element index till length. This pairs index to value in form of tuple list.
Python3
# Python3 code to demonstrate working of
# Convert list to indexed tuple
# Using zip() + range()
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
# Convert list to indexed tuple
# Using zip() + range()
res = list(zip(range(len(test_list)), test_list))
# Printing result
print("List after conversion to tuple list : " + str(res))
Output :
The original list : [4, 5, 8, 9, 10]
List after conversion to tuple list : [(0, 4), (1, 5), (2, 8), (3, 9), (4, 10)]
Method #3 : Using lists and append() method
Python3
# Python3 code to demonstrate working of
# Convert list to indexed tuple
# initializing list
test_list = [4, 5, 8, 9, 10]
# printing list
print("The original list : " + str(test_list))
res=[]
for i in range(0,len(test_list)):
res.append((i,test_list[i]))
# Printing result
print("List after conversion to tuple list : " + str(res))
OutputThe original list : [4, 5, 8, 9, 10]
List after conversion to tuple list : [(0, 4), (1, 5), (2, 8), (3, 9), (4, 10)]
Similar Reads
Python | Convert Integral list to tuple list Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
3 min read
Convert List to Tuple in Python The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 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 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 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
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
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
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
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