
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert a list into tuple of lists in Python
Converting one data container into another in python is a frequent requirement. In this article we will take a list and convert into a tuple where each element of the tuple is also a list.
With tuple
We can apply the tuple function straight to the list. But we have to also put a for loop in place so that each element is enclosed in a [].
Example
listA = ["Mon",2,"Tue",3] # Given list print("Given list A: ", listA) # Use zip res = tuple([i] for i in listA) # Result print("The tuple is : ",res)
Output
Running the above code gives us the following result −
Given list A: ['Mon', 2, 'Tue', 3] The tuple is : (['Mon'], [2], ['Tue'], [3])
With zip and map
We can also use zip and map in a similar approach as in the above. The map function will apply the list function to each element in the list. Finally the tuple function converts the result into a tuple whose each element is a list.
Example
listA = ["Mon",2,"Tue",3] # Given list print("Given list A: ", listA) # Use zip res = tuple(map(list, zip(listA))) # Result print("The tuple is : ",res)
Output
Running the above code gives us the following result −
Given list A: ['Mon', 2, 'Tue', 3] The tuple is : (['Mon'], [2], ['Tue'], [3])
- Related Articles
- Convert list into list of lists in Python
- Python - Convert a list of lists into tree-like dict
- How to convert a list into a tuple in Python?
- Python program to convert a list into a list of lists using a step value
- How to convert a list of lists into a single list in R?
- Python - Ways to iterate tuple list of lists
- Convert two lists into a dictionary in Python
- Python - Convert List of lists to List of Sets
- Python program to convert Set into Tuple and Tuple into Set
- Convert list of tuples into list in Python
- Unpacking tuple of lists in Python
- Convert a string representation of list into list in Python
- Python - Convert column to separate elements in list of lists
- Convert set into a list in Python
- How to convert JSON data into a Python tuple?

Advertisements