user5947793
user5947793

Reputation:

Function python list (zip)

This are 2 lists:

a= [[1,2,3], [4,5,6]]
b= ["Python", "Java"]

So i want this result:

X = [[1,2,3], "python"]
Y = [[4,5,6], "Java"]

It must become a function: e.g.:

def zipp (a,b):
  for same in zip(a,b):
     return (i)

print (zipp(a,b))

But, when i replace the return (i) in the function by print (i), it prints what i want... but unfortunately it only returns 1 of the 2. How can i return both?

Upvotes: 0

Views: 259

Answers (2)

Hsiao Yi
Hsiao Yi

Reputation: 135

I think you do not understand how the function works. When you call a function, if the function returns a value, the loops will not continue. The i are not simultaneously returned.

Upvotes: 0

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

You can do this:

>>> def zipp (a,b):
...    return [[x,y] for x, y in zip(a, b)]

>>> zipp(a, b)
[[[1, 2, 3], 'Python'], [[4, 5, 6], 'Java']]

It must become a function...

Note that you use zip directly, without defining any extra functions:

>>> print(list([x, y] for x, y in zip(a, b)))
[[[1, 2, 3], 'Python'], [[4, 5, 6], 'Java']]

Upvotes: 2

Related Questions