MTG
MTG

Reputation: 301

Concatenate a list of lists in a list Python3

i tried itertools,map() but i don't knoow what wrong. Ihave this:

[['>Fungi|A0A017STG4.1/69-603 UP-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', ['-', '-', '-', ... , '-', '-', '-', '-']],['>Fungi|A0A017STG4.1/69-603 UP1-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', ['-', '-', '-', ... , '-', '-', '-', '-']],['>Fungi|A0A017STG4.1/69-603 UP12-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', ['-', '-', '-', ... , '-', '-', '-', '-']]]

I want this:

[['>Fungi|A0A017STG4.1/69-603 UP-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}','-', '-', '-', ... , '-', '-', '-', '-'],['>Fungi|A0A017STG4.1/69-603 UP1-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}','-', '-', '-', ... , '-', '-', '-', '-'],['>Fungi|A0A017STG4.1/69-603 UP10-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}','-', '-', '-', ... , '-', '-', '-', '-']]

I tried

for i in x:
    map(i,[])

and this

import itertools
a = [["a","b"], ["c"]]
print list(itertools.chain.from_iterable(a))

pls enlighten me!

Upvotes: 0

Views: 1910

Answers (2)

Serge
Serge

Reputation: 3765

simple oneliner can do:

[sum(x, []) for x in yourlist]

note sum(x, []) is rather slow, so for serious list merging use more fun and lighting fast list merging techniques discussed at

join list of lists in python

for example, simple two liner is way faster

import itertools
map(list, (map(itertools.chain.from_iterable, yourlist)))

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98871

There must be better Pythonic solutions, but you can use:

n = []
for x in your_list:
    temp_list = [x[0]]
    [temp_list.append(y) for y in x[1]]
    n.append(temp_list)

print(n)

Outputs:

[['>Fungi|A0A017STG4.1/69-603 UP-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', '-', '-', '-', Ellipsis, '-', '-', '-', '-'], ['>Fungi|A0A017STG4.1/69-603 UP1-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', '-', '-', '-', Ellipsis, '-', '-', '-', '-'], ['>Fungi|A0A017STG4.1/69-603 UP12-domain-containing protein {ECO:0000313|EMBL:EYE99555.1}', '-', '-', '-', Ellipsis, '-', '-', '-', '-']]

Upvotes: 1

Related Questions