0

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!

1
  • If a tuple is what you actually want, just use the zip () function directly. It returns back a list of tuples. Commented Feb 18, 2016 at 19:55

2 Answers 2

3

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']]]
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Ty, but this prints: new1 as: ([1, 2, 3, 4, 5], 'h') instead of ([1, 2, 3, 4, 5], 'hallo")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.