5

I'm doing some exercises in Python and I came across a doubt. I have to set a list containing the first three elements of list, with the .append method. The thing is, I get an assertion error, lists don't match. If I print list_first_3 I get "[['cat', 3.14, 'dog']]", so the double square brackets are the problem. But how can I define the list so the output matches?

list = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
list_first_3.append(list[:3])

    
assert list_first_3 == ["cat", 3.14, "dog"]
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
santoros
  • 85
  • 1
  • 1
  • 8
  • 2
    Use `list_first_3 = list[:3]`. This will set the value of `list_first_3` to what you want it to be. – Henry Sep 19 '21 at 20:11
  • @Henry This is would work in this case specifically but not generally for adding. – Honn Sep 19 '21 at 20:13
  • 1
    Does this answer your question? [What is the difference between Python's list methods append and extend?](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) – mkrieger1 Sep 19 '21 at 20:16
  • Thanks for the info, I got the difference. I was confused because the rules of the exercise didn't mention a for loop to solve it, just. append – santoros Sep 19 '21 at 20:21
  • One more very important statement. You should never ever ever name your variables with reserved words or built-ins. See a way to get this list here: https://stackoverflow.com/a/22864250/2831135 – Nikolay Grischenko Sep 27 '21 at 07:11
  • Thanks for the info. The code was in another language and I made a fast translation to show it here, I didn't think of that then. – santoros Sep 28 '21 at 12:31

3 Answers3

11

append can only add a single value. I think what you may be thinking of is the extend method (or the += operator)

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
list_first_3.extend(list1[:3])

assert list_first_3 == ["cat", 3.14, "dog"]

or

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
list_first_3 += list1[:3]

assert list_first_3 == ["cat", 3.14, "dog"]

otherwise you'll need a loop:

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
for value in list1[:3]: list_first_3.append(value) 

assert list_first_3 == ["cat", 3.14, "dog"]

with append but without a loop would be possible using a little map() trickery:

list1 = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
any(map(list_first_3.append,list1[:3]))

assert list_first_3 == ["cat", 3.14, "dog"]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
2

When appending a list to a list, the list becomes a new item of the original list:

list_first_3 == [["cat", 3.14, "dog"]]

You are looking for:

list_first_3 += list[:3] # ["cat", 3.14, "dog"]

This adds every item from list to list_first_3.

Also you shouldn't name your variables like inbuilt types like list.

If you NEED to append, you could use a for-loop:

list_first_three = []
for item in list[:3]:
    list_first_three.append(item)
Honn
  • 737
  • 2
  • 18
1

Your problem is that you try to append a list to another list.

list[:3] will return you the result ["cat", 3.14, "dog"] Then, you take that result as a whole and put it as an item in list_first_3. If you want to fix that, you can do:

list = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []
list_first_3 += list[:3]

assert list_first_3 == ["cat", 3.14, "dog"] # Return True
       

And if you insist using append method:

list = ["cat", 3.14, "dog", 81, 6, 41]
list_first_3 = []

for item in list[:3]
    list_first_3.append(item)

assert list_first_3 == ["cat", 3.14, "dog"] # Return True
Eladtopaz
  • 1,036
  • 1
  • 6
  • 21
  • Thanks a lot. I'm not insisiting lol, it was requirement of the exercise. So the only way is with the for loop. – santoros Sep 19 '21 at 20:17