Skip to content
Related Articles

Related Articles

Python – Convert List to Single valued Lists in Tuple
  • Last Updated : 10 Jul, 2020

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().




# 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)) 
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().




# 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)) 
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :