-5

How can I reach the following goal??

Write a function join which, given two lists, it returns a list in which each element is a list of two elements, one from each of the given lists. For example:

join( [1,2,3] , [”a”,”b”,”c”] )

returns

[ [1,”a”], [2,”b”], [3,”c”] ]

assume that the given lists both have the same length

Mohd
  • 5,523
  • 7
  • 19
  • 30
xEno
  • 9
  • 2

2 Answers2

1

You can use zip:

def join(a, b):
    return [[i, c] for i, c in zip(a, b)]

print join([1,2,3], ['a','b','c'])

Output:

[[1, 'a'], [2, 'b'], [3, 'c']]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

Use zip:

def join(list_a, list_b):
    return [list(tup) for tup in zip(list_a, list_b)]



a = [1,2,3]
b = ["a", "b", "c"]
c = join(a, b)
print c #prints [[1, "a"], [2, "b"], [3, "c"]]