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 : Using list comprehension
List comprehension can be used as a shorthand method to solve this particular problem. In this, we iterate through the list and convert each integer element into a tuple object.
# Python3 code to demonstrate working of # Convert Integral list to tuple list # using list comprehension # initialize list test_list = [1, 4, 6, 8, 9] # printing original list print("The original list : " + str(test_list)) # Convert Integral list to tuple list # using list comprehension res = [(ele, ) for ele in test_list] # printing result print("List after conversion to tuple list : " + str(res)) |
The original list : [1, 4, 6, 8, 9]
List after conversion to tuple list : [(1, ), (4, ), (6, ), (8, ), (9, )]
Method #2 : Using zip() + list()
This is the simplest method to perform this particular task. The zip() when applied to list of integers, it converts each element to tuple. It returns a zip object and hence it has to converted back to list using list().
# Python3 code to demonstrate working of # Convert Integral list to tuple list # using zip() + list() # initialize list test_list = [1, 4, 6, 8, 9] # printing original list print("The original list : " + str(test_list)) # Convert Integral list to tuple list # using zip() + list() res = list(zip(test_list)) # printing result print("List after conversion to tuple list : " + str(res)) |
The original list : [1, 4, 6, 8, 9]
List after conversion to tuple list : [(1, ), (4, ), (6, ), (8, ), (9, )]
Recommended Posts:
- Python - Convert Tuple Matrix to Tuple List
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python | Convert list to indexed tuple list
- Python | Convert mixed data types tuple list to string list
- Python | Convert List of Dictionary to Tuple list
- Python - Convert Tuple value list to List of tuples
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python | Convert String to tuple list
- Python | Convert a list into a tuple
- Python | Convert a list into tuple of lists
- Python | Convert list of tuple into dictionary
- Python - Convert List of Lists to Tuple of Tuples
- Python - Convert List to Single valued Lists in Tuple
- Python | sympy.Integral() method
- How to convert a list and tuple into NumPy arrays?
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.

