Concatenation of multiple lists with Python

Asked

Viewed 468 times

0

Good afternoon!

I have a problem and need help, I am working with 3 distinct lists that should be added to a dictionary, but so I can capture all values without one overwriting the other, I need to concatenate these 3 lists. I know that using ex: list1.extend(list2), I can concatenate, but I don’t know if the same applies to concatenation of multiple lists, is there any other way to accomplish this concatenation? Follows code structure, 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

2 answers

2

If the problem is just concatenating the lists, the @Gustavorps answer solves. If the problem is that there are lists within lists, such as:

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

Thus:

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

And then just concatenate. Example:

arrayDisciplinas = []

arrayDisciplinas.extend(monstrinho_em_lista(disciplina1))
arrayDisciplinas.extend(monstrinho_em_lista(disciplina2))

1


According to question en-us: How to merge Multiple lists into one list in python? [Uplicate] cited 3 forms

Form 1: Importing the itertools

import itertools

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

Form 2: Using the extend

As you have already quoted.

arrayDisciplinas = []

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

Shape 3: Using Operated +

disciplina + disciplina2 + disciplina3
  • Thank you so much for your help, really the best situation for my case was using form 2, following the tutorial of Gustavorps :).

Browser other questions tagged

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