Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a tuple into array in Python?

Tags:

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.

like image 965
prosseek Avatar asked Sep 10 '10 19:09

prosseek


People also ask

Is it possible to create an array from a tuple?

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

Can you convert a tuple to a list?

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.

How do you convert a tuple in Python?

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.


1 Answers

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 tupleas the name of a variable.

like image 54
Mark Byers Avatar answered Oct 07 '22 23:10

Mark Byers