Python | Convert list to indexed tuple list
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 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 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)) |
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 #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 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)) |
The original list : [4, 5, 8, 9, 10] List after conversion to tuple list : [(0, 4), (1, 5), (2, 8), (3, 9), (4, 10)]
Recommended Posts:
- Python | Convert mixed data types tuple list to string list
- Python | Convert Integral list to tuple list
- Python | Convert a list into a tuple
- Python | Convert list of tuple into dictionary
- Python | Convert a list into tuple of lists
- Python | Convert String to tuple list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Pair and combine nested list to tuple list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Sort tuple list by Nth element of tuple
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string List to Nested Character List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

