6

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"] 
6
  • 2
    Use list_first_3 = list[:3]. This will set the value of list_first_3 to what you want it to be. Commented Sep 19, 2021 at 20:11
  • @Henry This is would work in this case specifically but not generally for adding. Commented Sep 19, 2021 at 20:13
  • 1
    Does this answer your question? What is the difference between Python's list methods append and extend? Commented Sep 19, 2021 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 Commented Sep 19, 2021 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: stackoverflow.com/a/22864250/2831135 Commented Sep 27, 2021 at 7:11

3 Answers 3

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"] 
Sign up to request clarification or add additional context in comments.

Comments

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) 

3 Comments

Thanks, I get that. The thing is, using .append is mandatory in this exercise. I guess is it impossible to do it with this method?
@santoros see my edit
Thanks! that's what I needed
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 

1 Comment

Thanks a lot. I'm not insisiting lol, it was requirement of the exercise. So the only way is with the for loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.