Doubt - insert element at the end of the list - (Python)

Asked

Viewed 661 times

0

I’m new here and I’m learning Python now. I have a question here of how to insert an element at the end of the list. I searched on the Internet as if to insert some element at the end of the list, and saw that uses the append() method. But since I’m a beginner yet, I don’t know if it’s right as I’m doing...

class LL:

    def__init__(self).inicio = None

    def taVazia(self): return self.inicio is None

    def insereInicio(self, item):

        temp = Noh(item)
        temp.setProx(self.inicio)
        self.inicio = temp

    def insereFim(self, item):
        temp = Noh(item)
        temp.setAnterior(self.fim)
        self.fim = temp
        append(novoItem)

3 answers

3

So Leonardo, you need to call the function .append() after the list variable, like this(In Python 3):

lista = [1,2,3,4,5]
print(lista)    # Resultado: [1, 2, 3, 4, 5]

lista.append(6) # Resultado: [1, 2, 3, 4, 5, 6]
print(lista)

0

Use the method .append()

arr = [1, 2, 3]

arr.append(4)  # [1, 2, 3, 4]

as lists are heterogeneous structures, you can store strings (words or text in simple terms). Using the same list:

arr.append("olá")  # [1, 2, 3, 4] antes do comando ser executado
print(arr)  # [1, 2, 3, 4, "olá"]

0

The method .append() creates a space that will be the last position on the list.

Ex.:

lista = [1, 2]
lista.append(3)  #resultado: [1, 2, 3]
  • I gave a small reworked in your publication. If you find it inappropriate, you can revert to revision. (although it is the same solution as the above-mentioned colleague)

Browser other questions tagged

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