Python – Convert List to Single valued Lists in Tuple

Conversion of data types is the most common problem across CS domain nowdays. One such problem can be converting List elements to single values lists in tuples. This can have application in data preprocessing domain. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 3, 5, 6, 7, 9]
Output : ([1], [3], [5], [6], [7], [9])

Input : test_list = [1]
Output : ([1])

Method #1 : Using list comprehension + tuple()
This is one of the ways in which this task can be performed. In this, we construct and iterate the list using list comprehension and finally convert the result list to tuple using tuple().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert List to Single valued Lists in Tuple
# Using list comprehension + tuple()
  
# initializing lists
test_list = [6, 8, 4, 9, 10, 2]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Convert List to Single valued Lists in Tuple
# Using list comprehension + tuple()
res = tuple([ele] for ele in test_list)
  
# printing result 
print("Tuple after conversion : " + str(res)) 

chevron_right


Output :



The original list is : [6, 8, 4, 9, 10, 2]
Tuple after conversion : ([6], [8], [4], [9], [10], [2])

 

Method #2 : Using map() + list() + zip() + tuple()
The combination of above functions can be used to solve this problem. In this, we perform task of extending logic of conversion to each element using map(), and list conversion to each element using zip(). At end, the result is converted back to tuple using tuple().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert List to Single valued Lists in Tuple
# Using map() + list() + zip() + tuple()
  
# initializing lists
test_list = [6, 8, 4, 9, 10, 2]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Convert List to Single valued Lists in Tuple
# Using map() + list() + zip() + tuple()
res = tuple(map(list, zip(test_list)))
  
# printing result 
print("Tuple after conversion : " + str(res)) 

chevron_right


Output :

The original list is : [6, 8, 4, 9, 10, 2]
Tuple after conversion : ([6], [8], [4], [9], [10], [2])

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.