0

I want to join two lists in such a way that the elements become lists

input:

a = [1,2,3,4,5,6,7]
b = [a,b,c,d,e,f,g]

output:

c = [[1,a],[2,b],[3,c],[4,d],[5,e],[6,f],[7,g]]

so far I tried to do this:

product = []
price = []

p = []
for g, h in zip(product, price):
    p.append([])
    for l in range(0, len(p)):
        p[l].append(g)
        p[l].append(h)
print(p)

1 Answers1

0

This code is working as you expected

a = [1, 2, 3, 4, 5, 6, 7]
b = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
ab = zip(a, b)
lis_com = [list(i) for i in ab]
print (lis_com)

Output:-

[[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e'], [6, 'f'], [7, 'g']]

ImMJ
  • 206
  • 2
  • 5