1

Using Python, I would like to be able to add multiple values/elements/items to a single list without having to repeat the .append method

Here is an example of what I mean. Review the following Python function:

singleList = []

def cal(rock):
    singleList.append(rock)
    singleList.append(input('Size: '))
    singleList.append(int(input('Qty: ')))
    print(singleList)

rock = cal(input('Rock: '))

Rock: granite
Size: small
Qty: 2

['granite', 'small', 2]

The -- singleList.append(list_element) -- statement must be repeated each time. Is it not possible to do something more like this:

singleList = []

def cal(rock):
    singleList.append(rock), input('Size: '), int(input('Quantity: '))
    print(singleList)

rock = cal(input('Rock: '))

...and still end up with:

['granite', 'small', 2]

Every way that I try to do this, I get the following error.

TypeError: append() takes exactly one argument (3 given)

I understand that .append() can only accept one argument, as per the error message. Is there an alternative way around this, or do people just repeat the .append() statement for each argument/element/value/item?

P.S. anyone know the correct term for: argument/element/value/item?

  • 5
    Look at `.extend()` (`help(list.extend)`). (There’s also `+=` but it can be a little confusing.) – Ry- Aug 17 '18 at 21:14
  • First, you need to be careful about parentheses. `singleList.append(rock), …` is only passing `rock` to `append`. Whatever comes after that `,`, you're just building a tuple out of the result of calling `append(rock)` and those other values. You are _not_ passing 3 arguments to `append` in that code. If you want to pass multiple arguments, you have to put them all inside the `()`. If you want to pass a list, tuple, or other collection as an argument, you have to put the whole collection inside the `()`. – abarnert Aug 17 '18 at 21:17
  • One you fix _that_ problem, then you'll be trying to `append` a list or tuple of three values to `singleList`, which will _work_, but do the wrong thing. That's when switching to `extend` will solve your problem. – abarnert Aug 17 '18 at 21:18
  • Meanwhile, your function doesn't return anything; it just modifies `singleList` and then `print`s it. So, after that call, `rock` is just going to be `None`. – abarnert Aug 17 '18 at 21:19

3 Answers3

3

You can do

singleList.extend(
                 [rock, input('Size: '), int(input('Quantity: '))]
                 )

or equivalently

singleList += [rock, input('Size: '), int(input('Quantity :'))]

which is syntactic sugar for singleList = singleList + <other list> and where Python interprets the + using the __iadd__ method, more details can be found here if you're interested. As pointed out in the comments, this will mutate the list in place, so you can't use it directly in a print or return statement.

Zain Patel
  • 983
  • 5
  • 14
1

You have mainly two options:

1) Use extend:

singleList.extend([rock, input('Size: '), int(input('Quantity: '))])

2) Concatenate your list with another list:

singleList += [rock, input('Size: '), int(input('Quantity: '))]

They are quite close in performance:

$ python -m timeit 'l = [1,2,3]; l.extend([4,5,6,7,8,9])'
1000000 loops, best of 3: 0.553 usec per loop

$ python -m timeit 'l = [1,2,3]; l += [4,5,6,7,8,9]'
1000000 loops, best of 3: 0.493 usec per loop
Jundiaius
  • 6,214
  • 3
  • 30
  • 43
0
singleList.extend(
    [rock, input('Size: '), int(input('Quantity: '))]
)
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – 31piy Aug 18 '18 at 05:21