How can I convert a Python tuple to an Array?

PythonProgramming

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]
raja
Published on 18-Apr-2018 15:01:02
Advertisements