Python Python Basics (2015) Python Data Types Lists

help with this please

when i type my list like this for example

list=(1, 2, 3, 4) return
(1, 2, 3, 4)
list + [5, 6]

it says error someone help me please

[MOD: added formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 57,147 Points

Be careful not to use built-in keywords as variable names. list is a keyword. By using it as a variable name it can not be used to create a new list using the constructor list().

>>> list
<class 'list'>
>>> list = (1, 2, 3, 4)
>>> list
(1, 2, 3, 4)
>>> type(list)
<class 'tuple'>

By using parens instead of square brackets or braces [ ], you've created a tuple. A tuple can not be changed, that is, it's immutable.

Using list + [5, 6] you are attempting to concatenate a tuple with a true list. This is not possible.

>>> list + [5, 6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple

Perhaps, try:

items= list((1, 2, 3, 4))
# or
items = [1, 2, 3, 4]
# then use
items + [5, 6]

Post back if you need more help. Good luck !!

Haydar Al-Rikabi
Haydar Al-Rikabi
5,967 Points

Chris Freeman I tried your suggestion to create a list as such:

items= list(1, 2, 3, 4)

but it is returning the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (4 given)

Aren't we supposed to pass an iterable to list(), for example a tuple:

items =  list((1, 2, 3, 4))