Using Append - Python

Asked

Viewed 3,255 times

-5

What is the need to use the "Append" function in my code? They say it can be used to add another element to my Python list, but wouldn’t it be simpler to add this other element in a traditional way? Append only serves for this "addition"?

  • 1

    What would be the "traditional form"?

  • Just writing the element together with the others who are part.

2 answers

6


Add an element to a list dynamically and there are several reasons why this is possible:

  • Do not know previously the value to be added;
  • Make sure your list has room for the new value;

And possibly other reasons.

Do not know previously the value to be added

Let’s say I need to ask my user for a list of 5 integer values. How can I create this list if I don’t know what values the user will enter? So I need to create my list and insert the values dynamically:

numeros = []

for _ in range(5):
    numeros.append(int(input('Digite um número: '))

print(numeros)

With this, no matter what values the user enters, the program will add in the list in the same way.

>>> Digite um número: 1
>>> Digite um número: 2
>>> Digite um número: 3
>>> Digite um número: 4
>>> Digite um número: 5
[1, 2, 3, 4, 5]

That means I’ll always use the append? No. Many problems can be solved in other ways. It’s up to you, as a developer, to evaluate which one is ideal. If you want a list with 5 user read values, could do:

numeros = [int(input('Digite um número: ')) for _ in range(5)]

The result is exactly the same as the previous one, without using the append. If I just want the even values, as quoted in the other answer, you could do:

pares = [numero for numero in range(20) if numero % 2 == 0]

Same result without using the append.

Even, it is often more feasible for you to do without the append, for he spends memory for nothing.

Make sure your list has room for the new value

If my list has 5 elements, it will occupy in memory the equivalent of 5 times the memory that is demanded by that type of object. If I need to add a sixth element to the list, what assures me that there is memory available? The method append will ensure this for you as it will perform what we call dynamic memory allocation. Whenever you insert a new element it will be responsible for allocating memory so that it is possible to save the new value.

This dynamic allocation of append, even, seeks to work in an optimized way. How to make the memory allocation for each new value can be costly for application, instead of allocating only a new space will be allocated several, for if added more values already exist available memory. This is interesting if your list undergoes many modifications, but is bad in opposite cases as it can be allocated much more memory than was actually needed.

I commented on this behavior in:

For this reason it is common for you to read that it is best for you to use the list-comprehension (above solutions without the append) than the solutions that use the append. But this is not always valid; there are problems that it is better to use the append - otherwise he wouldn’t even exist.

2

It is recommended to use the append when you use a loop to write this data in a list, example: In this example I want only the even numbers from 0 to 20

lista_par = []
for i in range(20):
   if i%2 == 0:
      lista_par.append(i)
print(lista_par)   

Output = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

  • 1

    Who recommends it? Why is it recommended?

  • When you use the assignment signal you override the object, when you use the append you add the element at the end of the list, the index control is performed automatically, since when you use only the simple assignment signal, the same "=" you must perform this control, adding an index and incrementing, in the example above if you use the "=" would look like this "list_par[i] = i" inside the for block.

  • 2

    The answer is not wrong, but I felt obliged to vote against it because I felt that there was no basis. If you edit the answer and supplement it with the arguments that defend your answer, I will be happy to reconsider the vote. In the comments you even cite some reasons, so that should be in the answer. It would be interesting to also explain why overwriting the object is worse than changing it. Always this is true?

  • I’m new here in stack overflow with one that helps solve doubts, I’m still getting the hang of explaining things, mainly programming. Thanks for the feedback, I will improve and next time I hope to do correctly!

  • Welcome, then :D Anything, we are always online at chat community if you want to ask any questions.

  • Thank you very much !

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.