Concatenating multiple lists with Python

0

Good afternoon!

I have a problem and need help, I'm working with 3 distinct lists that should be added to a dictionary, but so I can capture all the values without one overwriting the other, I need to concatenate those 3 lists. I know that using ex: list1.extend(list2) , I can concatenate, but I do not know if the same applies to concatenating multiple lists, is there any other way to do this concatenation? It follows the structure of the code, already showing the dictionary.

arrayDisciplinas = []
if response.xpath('//*[contains(a,"PDL")]//following-sibling::div//text()[not(re:test(.,"^\s+$"))]').extract() is not None:
    for linha in response.xpath('//*[contains(a,"PDL")]//following-sibling::div//text()[not(re:test(.,"^\s+$"))]').extract():
        disc = Disciplina()
        disc.set_all()
        if linha is not None:
            disc['Nome'] = linha.encode('utf-8')
        arrayDisciplinas.append(dict(disc))
        if len(arrayDisciplinas) > 0:
            disciplina = arrayDisciplinas

arrayDisciplinas2 = []
if response.xpath('//*[contains(a,"CLEO ESP")]//following-sibling::div//text()[not(re:test(.,"^\s+$"))]').extract() is not None:
    for linha in response.xpath('//*[contains(a,"CLEO ESP")]//following-sibling::div//text()[not(re:test(.,"^\s+$"))]').extract():
        disc2 = Disciplina()
        disc2.set_all()
        if linha is not None:
            disc2['Nome'] = linha.encode('utf-8')
        arrayDisciplinas2.append(dict(disc2))
        if len(arrayDisciplinas) > 0:
            disciplina2 = arrayDisciplinas2

arrayDisciplinas3 = []
if response.xpath('//*[contains(a,"CLEO ESTRUTURANTE")]//following-sibling::div//text()[not(re:test(.,"^\s+$"))]').extract() is not None:
    for linha in response.xpath('//*[contains(a,"CLEO ESTRUTURANTE")]//following-sibling::div//text()[not(re:test(.,"^\s+$"))]').extract():
        disc3 = Disciplina()
        disc3.set_all()
        if linha is not None:
            disc3['Nome'] = linha.encode('utf-8')
        arrayDisciplinas3.append(dict(disc3))
        if len(arrayDisciplinas) > 0:
            disciplina3 = arrayDisciplinas3

disciplina.extend(disciplina2)

item['Disciplinas'] = disciplina3
    
asked by anonymous 08.08.2018 / 20:14

2 answers

1

According to the question in en-us: How to merge multiple lists into one list in python? [duplicate] quoted 3 ways

Form 1: Importing itertools

import itertools

iterable_disciplinas = itertools.chain(disciplina, disciplina2, disciplina3)
list(iterable_disciplinas)

Form 2: Using extend

As you have already mentioned.

arrayDisciplinas = []

arrayDisciplinas.extend(disciplina)
arrayDisciplinas.extend(disciplina2)
arrayDisciplinas.extend(disciplina3)

Form 3: Using operated +

disciplina + disciplina2 + disciplina3
    
08.08.2018 / 21:17
1

If the problem is just concatenating the lists, the response from @GustavoRPS resolves. If the problem is there are lists within lists, like:

disciplina1 = [1, [2,3]]
disciplina2 = [[4,5], 6, [[7,[8,9]]]]

So you can break these little monsters into a single list like this:

def monstrinho_em_lista(lista):
    lista_final = []
    for item in lista:
        if isinstance(item, list):
            lista_final.extend(monstrinho_em_lista(item))
        else:
            lista_final.append(item)
    return lista_final

So:

>>> monstrinho_em_lista(disciplina2)
[4, 5, 6, 7, 8, 9]

And here's how to concatenate. Example:

arrayDisciplinas = []

arrayDisciplinas.extend(monstrinho_em_lista(disciplina1))
arrayDisciplinas.extend(monstrinho_em_lista(disciplina2))
    
08.08.2018 / 21:30