2
I need HELP!!

WHAT DOES THE .append thing do?

You are viewing a single comment. View All
Answered by
TheDrone7 (498)
[earned 5 cycles]
View Answer
1

Hello, I see you have got your answer although if you want more detail you can continue reading my answer: The .append() function is used when you want to add a new element/item/thing to a list. Eg. You append/add a new name to a list of boys names or add a username to a list of already taken/used usernames, like Instagram would when you create a new account, as an example. So to add a new element/item/thing to a list is to append (add) the item to the list. When you append an item to a list, the new element is added to the end of the list. If I put this in an example, let's say you want to make a list of possible baby names for a boy and you want to append (add) a new name that you like to this list, you would do this:

boy_names = ['khaled', 'amir', 'malik'] # this creates the list
print(boy_names)

*** this would print: ['khaled', 'amir', 'malik'] ***

boy_names.append('ali') # You use the variable name then put .append() after the variable name. Then put the name/element/item you want to add to the list using quotes around the name and put it in the brackets it will add the new name to the end of the list. Note: you use quotes because it's a string (text). If you're doing a list in which you're using numbers and want to keep them as integers or floats etc (eg if you want to add and multiply them) you don't need the quotes.
print(boy_names)

*** this would print: ['khaled', 'amir', 'malik', 'ali'] *** as you can see the name 'ali' is added to the end of the list now and the variable boys_names will always have 'ali' in the list when it gets printed or used in the program etc since it got appended/added (unless​​ you remove it).

THE CODE WITHOUT THE COMMENTS TO MAKE IT VISUALLY EASIER TO UNDERSTAND:

boy_names = ['khaled', 'amir', 'malik']
print(boy_names)

boy_names.append('ali')
print(boy_names)

If you want to see this run you can click on the link and see it work in my repl: https://repl.it/@zcses/append

So I hope this clears up your problem, and if you have any questions feel free to ask :)