In this Python tutorial, we will discuss how to convert numpy tuple to a list in Python using different methods and examples.
- How to convert NumPy tuple to list in Python using list() function
- How to convert NumPy tuple to list in Python using the append() method
- How to convert NumPy tuple to list in Python using extend() method
- How to convert NumPy tuple to list in Python using the * operator
How to convert NumPy tuple to list in Python
NumPy does not offer a built-in function to create tuples directly, so we need to create a NumPy array first and then use the Python built-in function tuple() to convert the array into a tuple.
Then convert the tuple to a list, the methods are discussed below which show how to convert numpy tuple to a list.
Method-1: Convert NumPy tuple to list using list() function
You can use the built-in list() function to convert a tuple to a list.
# import numpy library
import numpy as np
# create a tuple using numpy
t = tuple(np.array(['USA', 'United Kingdom', 'Canada']))
# convert the tuple to a list
l = list(t)
# Print the list
print(l)
The above code demonstrates how to create a tuple using the NumPy library, and then convert that tuple into a list.
- Next, a tuple t is created using NumPy’s array() function. The array() function is passed a list of strings, which it converts to a NumPy array.
- Then, the tuple() function is called on this NumPy array to create a tuple t containing the same elements.
- After that, the list() function is called on the tuple t to convert it into a list l.
- Finally, the list l is printed to the console using the print() function.
Method-2: Convert NumPy tuple to list using append() method
You can create an empty list and use the append() method to add each element of the tuple to the list.
# create a tuple
import numpy as np
# create a tuple using numpy
t = tuple(np.array([22, 23, 34]))
# create an empty list
l = []
# use append() to add each element of the tuple to the list
for i in t:
l.append(i)
# Print the list
print(l)
The above code demonstrates how to convert an array created using NumPy into a tuple using the tuple() function, and then convert that tuple into a list using a for loop and the append() method.
- Then, a NumPy array of integers is created using the np.array() function and assigned to the variable t.
- To convert the NumPy array into a Python tuple, the tuple() function is called on the NumPy array.
- An empty list is created using the list() function. To add each element of the tuple to the list, a for loop is used. The loop iterates over the elements of the tuple and the append() method is called on the list l to add each element to the list.
- Finally, the resulting list is printed to the console using the print(l) function.
Method-3: Convert NumPy tuple to list using extend()
You can also use the Python extend() method to add all the elements of the tuple to the list at once.
# import numpy library
import numpy as np
# create a tuple using numpy
t = tuple(np.array([11, 22, 33]))
# create an empty list
l = []
# use extend() to add all the elements of the tuple to the list
l.extend(t)
# print the list
print(l)
The above code uses the NumPy library to create an array of three elements. It then converts this array into a tuple using the tuple() function and assigns it to the variable t.
- It uses the extend() method to add all the elements of the tuple t to the end of the list l.
- Finally, the code prints the contents of the list l, which should contain the elements of the original NumPy array as individual elements in the list.
Method-4: Convert numpy tuple to list using the * operator
You can use the * operator to unpack the tuple and create a list with the same elements in Python.
# import numpy library
import numpy as np
# create a tuple using numpy
t = tuple(np.array(['USA', 'United Kingdom', 'Canada']))
# use the * operator to unpack the tuple and create a list
l = [*t]
# Print the list
print(l)
The above code is importing the NumPy library and then use it to create a tuple containing three strings.
- The np.array() function creates a NumPy array from the given list of strings, and the tuple() function converts the NumPy array into a tuple.
- Then, the
*
operator is used to unpack the tuple t and create a list l that contains the same elements as the original tuple. - Finally, the print() function is used to output the resulting list l to the console.
Output: ['USA', 'United Kingdom', 'Canada']
Conclusion
In this Python tutorial, we have covered 4 different methods to convert the numpy tuple to a list in Python.
- How to convert numpy tuple to list in Python using list() function
- How to convert numpy tuple to list in Python using the append() method
- How to convert numpy tuple to list in Python using extend() method
- How to convert numpy tuple to list in Python using the * operator
You may also like the following articles:
- How to Reverse NumPy array in Python
- Convert NumPy Array to List in Python
- Python NumPy Median() Function
- Numpy Divide in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.