How can I convert a Python tuple to an Array?


To convert a tuple to an array(list) you can directly use the list constructor. 

example

x = (1, 2, 3)
y = list(x)
print(y)

Output

This will give the output −

[1, 2, 3]

Example

If you have a multi-level tuple and want a flat array, you can use the following −

z = ((1, 2, 3), (4, 5))
y = [a for b in z for a in b]
print(y)

Output

This will give the output −

[1, 2, 3, 4, 5]

Updated on: 05-Mar-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements