21

Suppose I want a list of tuples. Here's my first idea:

li = []
li.append(3, 'three')

Which results in:

Traceback (most recent call last):
  File "./foo.py", line 12, in <module>
    li.append('three', 3)
TypeError: append() takes exactly one argument (2 given)

So I resort to:

li = []
item = 3, 'three'
li.append(item)

which works, but seems overly verbose. Is there a better way?

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
  • Hope that the following link will bring some light to you in about how to work with tuples: http://www.tutorialspoint.com/python/python_tuples.htm – Artsiom Rudzenka Jun 10 '11 at 10:09
  • 1
    @Artsiom Rudzenka - That tutorial is dangerous :( Not only does it have mistakes like mixing lists and tuples, it shows silly things like ending lines with `;` for some reason. – viraptor Jun 10 '11 at 10:14
  • 3
    @viraptor yes, you are right, it is always better to use official python tutorial: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences – Artsiom Rudzenka Jun 10 '11 at 10:16
  • 1
    Earlier versions of Python did convert the append(arg,...) syntax into append((arg,...)) automatically. They took it out because it added confusion. Old books would show your example as working. This is why it is best to check the documentation on the Python.org site or your installed Python distribution. – yam655 Jun 10 '11 at 10:51

5 Answers5

49

Add more parentheses:

li.append((3, 'three'))

Parentheses with a comma create a tuple, unless it's a list of arguments.

That means:

()    # this is a 0-length tuple
(1,)  # this is a tuple containing "1"
1,    # this is a tuple containing "1"
(1)   # this is number one - it's exactly the same as:
1     # also number one
(1,2) # tuple with 2 elements
1,2   # tuple with 2 elements

A similar effect happens with 0-length tuple:

type() # <- missing argument
type(()) # returns <type 'tuple'>
tzot
  • 92,761
  • 29
  • 141
  • 204
viraptor
  • 33,322
  • 10
  • 107
  • 191
  • 3
    It's worth pointing out explicitly that `(1)` is NOT a tuple. A pair of parentheses in that case is parsed for other syntax meanings (for order and precedence in math evaluations?). The syntax is different from `[1]` where it is a list. – neurite Mar 30 '18 at 00:17
8

It's because that's not a tuple, it's two arguments to the add method. If you want to give it one argument which is a tuple, the argument itself has to be (3, 'three'):

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> li = []

>>> li.append(3, 'three')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)

>>> li.append( (3,'three') )

>>> li
[(3, 'three')]

>>> 
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
6

Parentheses used to define a tuple are optional in return and assignement statements. i.e:

foo = 3, 1
# equivalent to
foo = (3, 1)

def bar():
    return 3, 1
# equivalent to
def bar():
    return (3, 1)

first, second = bar()
# equivalent to
(first, second) = bar()

in function call, you have to explicitely define the tuple:

def baz(myTuple):
    first, second = myTuple
    return first

baz((3, 1))
Simon Bergot
  • 10,378
  • 7
  • 39
  • 55
  • 2
    This helped me. I also found you can unpack earlier, eg `def baz((first, second)):` – djeikyb Jun 12 '12 at 16:24
  • Only if you are stuck in Python 2. See http://stackoverflow.com/questions/20035591/python-cant-define-tuples-in-a-function – Dave Aug 02 '16 at 18:26
0

it throws that error because list.append only takes one argument

try this list += ['x','y','z']

Argho Chatterjee
  • 579
  • 2
  • 9
  • 26
0
def product(my_tuple):
    for i in my_tuple:
        print(i)

my_tuple = (2,3,4,5)
product(my_tuple)

This how you pass Tuple as an argument

  • While this is completely true, this what the OP initially used which is very verbose. I'd suggest to elaborate on this and add some more information on applying this on the OP's problem with a list to improve your answer – user1781290 Jul 27 '19 at 08:50