Ask coding questions

← Back to all posts
2
I need HELP!!

WHAT DOES THE .append thing do?

Answered by
TheDrone7 (498)
[earned 5 cycles]
View Answer
Commentshotnewtop
4

You should really post more meaningful questions, like what language are you using, where have you seen the append function etc. Remember repl.it is a special platform for many languages not just python so be more specific. Now I'm assuming you're using python and the .append() function is used on list.

Let's suppose you have a list named l which is basically [5, 8, 9]. Just suppose, it can be anything.

Now if you do l.append(7) it becomes [5, 8, 9, 7].

2

Hi @Lanna -- @TheDrone7 is correct, you need to write your questions so people can understand what you are asking about.

Reading this post will help you ask better questions: https://repl.it/talk/ask/Guide-to-Asking-Coding-Questions/7251

1

@TheDrone7 So you mean that the .append adds a number to the end of the list?
Sorry for being unclear, I was in a rush.

2

@Lanna yes, it does and ok, but be sure to be more specific from the next time, also for such quick reference you can refer to python docs - https://docs.python.org/3/

1

@Lanna .append can also work for text too (eg: names). For more detail, you can look at my posted comment.

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 :)