In this tutorial, we’ll learn how to properly convert Python tuple to list with the help of multiple Python code examples.
After reading this post, I’m sure you’ll have a detailed practical knowledge of how to easily convert Python tuple to list.
Outline
- Introduction: Conversion of Python Tuple To List
- Convert Python Tuple To List (Multiple Examples)
- Syntax of list() Function
- Convert Python Tuple To List Inside Python For Loop
- Conclusion
Introduction: Conversion of Python Tuple To List
It specifies the type conversion of tuple in Python to Python list. We’ll be going through multiple examples on how to convert Python tuple to list for better understanding.
Let’s not keep ourselves waiting, so let’s jump right into its practical implementation.
Convert Python Tuple To List (Multiple Examples)
For that, we first have to create a simple Python tuple. See below code:
tupleItems=[1,2,3,4,5,6,7,8] // simple python tuple of integer items print(type(tupleItems))
Output
<class 'tuple'>
We now have a tuple of integers. In order to convert it to Python list, we’ve to use the list function.
Syntax of list() Function
list(tuple)
This function takes a tuple so it can convert it to Python list.
tupleItems=(1,2,3,4,5,6,7,8) print(tupleItems) print(type(tupleItems)) listItems=list(tupleItems) // tuple converted to list print(listItems) print(type(listItems))
Output
(1, 2, 3, 4, 5, 6, 7, 8) <class 'tuple'> [1, 2, 3, 4, 5, 6, 7, 8] // tuple is converted to list <class 'list'> // type shows that it is now a list
See below examples as well in which tuple of float items, strings, nested and mixed datatypes are also converted to Python list.
integerItems=(1,2,3,4,5,6,7,8) //tuple of integers floatItems=(1.3,2.2,4.7,4.9,5.22,6.44,7.2,8.1) //tuple of floats stringItems=('White','Green','Blue') // //tuple of strings nestedTupleItems=(2,3,(6,7,9),3,2,('green',4.5,22)) // //tuple of nested tuples mixedItems=([8,9,'orange'],222,543,'red',456.7,(1,2,4,(4.3,334))) // //tuple of mixed items # before conversion print(type(integerItems)) print(type(floatItems)) print(type(stringItems)) print(type(nestedTupleItems)) print(type(mixedItems)) # after conversion conversion1=list(integerItems) conversion2=list(floatItems) conversion3=list(stringItems) conversion4=list(nestedTupleItems) conversion5=list(mixedItems) print(type(conversion1)) print(type(conversion2)) print(type(conversion3)) print(type(conversion4)) print(type(conversion5))
Output
// this is before conversion <class 'tuple'> <class 'tuple'> <class 'tuple'> <class 'tuple'> <class 'tuple'> // this is after conversion <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>
Convert Python Tuple To List Inside Python For Loop
items=((1,2),(3.3,8.2),('white','blue'),(2,3,4,5.7,9.8,'green')) # before conversion for val in items: print(type(val)) # converting tuples to list newItem=[] // new list to store converted items(use it if you want) for val in items: newItem.append(list(val)) # showing converted types for val in newItem: print(type(val))
Output
// before conversion <class 'tuple'> <class 'tuple'> <class 'tuple'> <class 'tuple'> // after conversion <class 'list'> <class 'list'> <class 'list'> <class 'list'>
This is how we can easily convert Python tuple to list. Do try it with other examples as well. Hope you like this post.
Do ask if you still have any questions regarding the conversion of Python tuple to list. I’d be happy to answer all your questions.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly convert Python tuple to list. I’d be very happy to receive your feedback on this post.
Do visit my other articles on Python programming. Links to some of them are listed below. Others can be found using the navigation bar or search box of this site. Thank you for reading this post.
You may like to read:
How To Add Items In Python Tuple [2 Easy Ways]
How To Use Python String ZFill Method [Python Easy Guide]
How To Easily Use Python String Split Method [Detailed Python Guide]
How To Use Python String Format_Map Method [Python Easy Guide]
How To Use Python String IsDecimal() Method – Easy Python Code Guide
How To Easily Use Python String RPartition Method [Detailed Python Guide]
How To Use Python String Title Method [Python Easy Guide]
How To Easily Use Python String Replace Method [Detailed Python Guide]
How To Properly Use Python String StartsWith Method [Detailed Python Guide]
How To Change Flutter CircularProgressIndicator Stroke Width – Easy Flutter Example Code
How To Properly Use Python String RSplit Method [Detailed Python Guide]