user5947793
user5947793

Reputation:

Multiple lists in python

What i have:

a = [[1,2,3,4,5],[2,3,4,5]]


b = [["hallo"]],[["bye"]] 

What I want:

new1 = [[1,2,3,4,5],"hallo"]
new2 = [[2,3,4,5],"bye"]

the hardest part is that i want it in a way that when the user puts a extra list in a and b, it will not error but automatic add the new inputs to a new list (e.g. new 3 [with a [third], [b "third"]]

I hope someone can help me!

Upvotes: 0

Views: 34

Answers (2)

Reut Sharabani
Reut Sharabani

Reputation: 31339

You can use zip and unpacking:

>>> new1, new2 = list(zip(a, [x[0][0] for x in b]))
>>> new1
([1, 2, 3, 4, 5], 'hallo')
>>> new2
([2, 3, 4, 5], 'bye')

Obviously extra lists require that you adapt your code to n items and not assume two, but the trick is the same.

Upvotes: 1

Anton Protopopov
Anton Protopopov

Reputation: 31672

IIUC you could use zip function:

In [31]: new1, new2 = map(list, zip(a, b))

In [32]: new1
Out[32]: [[1, 2, 3, 4, 5], [['hallo']]]

In [33]: new2
Out[33]: [[2, 3, 4, 5], [['bye']]]

Upvotes: 3

Related Questions