22

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.

2
  • 2
    If you're passing your "array" (list) to a function that wants an iterable, virtually all of them will happily take a list, tuple, numpy.array, yourawesomeiterabletype, whatever. Commented Sep 10, 2010 at 19:40
  • 1
    First, Python doesn't have "array"s. It has sequences, including list and tuple. Why do you want to change one sequence into another? Commented Sep 10, 2010 at 19:46

1 Answer 1

69

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Correct answer and apt advice against using names of built in functions as variable names.
+1 As simple as this answer seems, it is not always obvious to n00bs in the Python world.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.