How to create a list and put other lists inside?

Asked

Viewed 4,177 times

2

def annexsentence():
        nome_tarefa=input("Nome da Tarefa :")
        data_atual = datetime.datetime.now().strftime("%d-%m-%y")
        data_final=input("Data Final :")
        estado="Aberta"
        lista=[nome_tarefa,str(data_atual),str(data_final),estado]
        annexsentence=open("ficheiro2.txt","a")
        annexsentence.write(str(lista)+"\n")
        annexsentence.close()
    annexsentence()

Good evening, I would like to know how to put this list I have already created inside another list getting for example:

[[lista1,lista1,lista1,],[lista2,lista2,lista2,],[lista3,lista3,lista3,]]

so that you can always add a new list within it

  • The variable lista is the second dimension right? And what is the list of the first dimension? Each dimension is a level, a dimension: [1,2,3], two-dimensional: [[1,2,3],[1,2,3],[1,2,3]], ...

  • i want to create an empty list for example list=[] and within the list put a 2 list with for example the [1,2,3],[1,2,3],[1,2,3] to stay [[1,2,3],[1,2,3],[1,2,3]]

  • @Pedropinheiro The solutions of the question must be in the answer area, not the question itself. If you have managed to solve the problem in a different way than those already addressed in the answers, feel free to answer your own question.

1 answer

3


Just create a variable of type list and pass a list in the method append:

lista = []
lista.append([1,2,3])
lista.append([1,2,3])
lista.append([1,2,3])
print lista
#[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

You can create a function to feed the list and another to save it, which will only call the saved function after you have fully filled the list:

lista = []

def annexsentence():
    nome_tarefa=input("Nome da Tarefa :")
    data_atual = datetime.datetime.now().strftime("%d-%m-%y")
    data_final=input("Data Final :")
    estado="Aberta"
    lista.append([nome_tarefa,str(data_atual),str(data_final),estado])

def salvar():
    annexsentence=open("ficheiro2.txt","a")
    annexsentence.write(str(lista)+"\n")
    annexsentence.close()

Or you can overwrite the contents of the file:

lista=[]

def annexsentence():
    nome_tarefa=input("Nome da Tarefa :")
    data_atual = datetime.datetime.now().strftime("%d-%m-%y")
    data_final=input("Data Final :")
    estado="Aberta"
    lista.append([nome_tarefa,str(data_atual),str(data_final),estado])
    annexsentence=open("ficheiro2.txt","w")
    annexsentence.write(str(lista)+"\n")
    annexsentence.close()
  • 1

    I tried to do that but then in the file it looks like this [['1', '01-01-19', '1', 'Open']][['1', '01-01-19', '1', 'Open']] and it doesn’t look like I want it nor with the commas it’s supposed to look like [['1', '01-01-19', '1', 'Open'] ['1', '01-01-19', '1'']]

  • 1

    You must write to the file only after having the list ready, the way you wrote to each sublist, I edited the answer

Browser other questions tagged

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