How to create a list and put other lists in?

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 that I created already inside another list for example:

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

so you can always add a new list inside it

    
asked by anonymous 01.01.2019 / 22:01

2 answers

3

Problem

Hello Peter, I understand that you would like to join several listas into another one that would be type macro , carrying the value of all others.

Possessing the following structure, regardless of the number of items included in them:

[[valores_lista_1], [valores_lista_2], [...]]

Lists can contain any mixes and combinations of data types, including other lists, thus allowing this crowding.

Solution:

A list can be created with harvests and can be created without content - to add later:

macro_list = []

And to do what you want, include other lists within another, you can use the append() method, which adds a new element to the end of the list.

I have exemplified below:

# criando as listas para serem incluídas
first_list = [1, 2, 3]
second_list = [9, 1, 7]
third_list = [12, 91, 72]

# criando a lista que possuirá todas as outras
macro_list = []

# utilizando o metodo append()
macro_list.append(first_list)
macro_list.append(second_list)
macro_list.append(third_list)

When we check the value that is in the list using the function print()

print(macro_list)

We get the following result:

[[1, 2, 3], [9, 1, 7], [12, 91, 72]]

Note: The values referring to the inclusion of the lists:

macro_list[0] é referente aos valores da first_list

macro_list[1] da second_list e

macro_list[2] da third_list

If you want to add a new list in the macro, just use the same function append() , including the next position with the desired values.

    
02.01.2019 / 00:01
2

Just create a variable of type list and pass a list in 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, it will only call the function that saves after filling the list completely:

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()
    
01.01.2019 / 22:47