Let's say I have a tuple t = (1,2,3,4)
. What's the simple way to change it into Array?
I can do something like this,
array = []
for i in t:
array.append(i)
But I prefer something like x.toArray() or something.
To convert a tuple to an array(list) you can directly use the list constructor.
Python list method list() takes sequence types and converts them to lists. This is used to convert a given tuple into list. Note − Tuple are very similar to lists with only difference that element values of a tuple can not be changed and tuple elements are put between parentheses instead of square bracket.
Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.
If you want to convert a tuple to a list (as you seem to want) use this:
>>> t = (1, 2, 3, 4) # t is the tuple (1, 2, 3, 4)
>>> l = list(t) # l is the list [1, 2, 3, 4]
In addition I would advise against using tuple
as the name of a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With