FAQ: Creating and Modifying a List in Python - Growing a List: Append

This community-built FAQ covers the “Growing a List: Append” exercise from the lesson “Creating and Modifying a List in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Growing a List: Append

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

Why is .append() used at the end of a line, instead of the usual function(parameters) format shown so far? Is there a specific name for functions that are formatted in this way?

2 Likes

I’m following this thread.

This is my code:

orders = ['daisies', 'periwinkle']
print(orders.extend(["violet", "rose"]))

Why does it return None? In Ruby we can link long methods like i’ve done above, so was wondering if there was a similar way to do it in Python as well.

This exercise suggests using the .append() function. Is there a difference between using this, or using the += function to add to the list. I know this works because it is how I’d been doing the exercises so far :sweat_smile:

empty_list = []
empty_list.append(1)

vs

empty_list = []
empty_list += [1]

Does it cause any difference to how the code is interpreted?
And is it not crazy to have another possible version without the ‘=’ sign:

empty_list + [1]
1 Like

Append is generally for adding a single extra element to a list. The operator += is generally (with some subtle differences) syntactic sugar for the list.extend() method instead which is used for adding multiple elements to the list.

Note the difference between append and += in that you cannot append the integer literal and it has to be a list (more generally it has to be iterable)-

lst = [0]
lst.append(1)  # this is fine
# lst += 2  # this would not work, int object is not iterable
lst += [2]  # acceptable because the list is iterable
# lst.extend(3)  # error, int obj not ierable
lst.extend((3, 4))  # tuple 3, 4 is iterable so we're good
print(lst)
Out: [0, 1, 2, 3, 4]

So, yes, there is a difference in how the code is interpreted (for .append, .extend and +=) but it’s not something you need to worry about all that much for the time being. As for subtle differences, there’s a SO question about this which goes into more detail (python2 rather than 3 so I’ve not checked if this has altered at all, probably not all that much)-

You could also create a list with + in the way you suggest but it’s not really very useful-

new_list = [] + [1, 2, 3]  # [] creates an empty list and + concatenates the two
# ... but why not just create a single new list?
new_list2 = [1, 2, 3]
new_list == new_list2  # equates to True

So there’s more than one way to perform the same action but aim for readability first and foremost.

1 Like

Can’t we just add something to the end of the list by typing it in the list itself like this:
orders = [‘daisies’, ‘periwinkle’, ‘tulips’, ‘roses’]
as apposed to:
orders.append( ‘tulips’, ‘roses’)
I tried it out and it seems to work. any explanations?

Hello @java3915118140 and welcome to the Codecademy Forums!

Yes, both methods will result in "tulips" and "roses" being added to the end of orders, but it is convention to use the .append() method in this situation. .append() is also helpful and more practical in many other situations.

It allows us to add items to the end of a list without having to type the entire list out. Say you had a list that contained twenty different objects. It would be tedious and impractical to append an object using your method. Additionally, .append() is useful when we may not know the full contents of the list. There are countless situations in which .append() would be useful, all of which I cannot mention here.

Aye, to add on to what @dr_victoria wrote, whilst you could hard-code new entries to a list by modifying your file you’re still putting all the work in yourself.

Whilst these examples might be short and sweet enough to simply edit the data yourself their purpose is to teach you the relevant commands in the Python language to automate these tasks. Initally that could simply save you a bit of effort but will very quickly enable you to complete tasks which would’ve been impossible otherwise, that’s just what programming is for.

If you’re actually just pointing out that you passed this task without append then you may well be right but you’d probably profit more from actually following the instructions :wink:.

1 Like