MHS
MHS

Reputation: 2350

How to merge multiple lists in python?

I have 2 lists:

c = [91.0, 92.0, 93.0, 94.0]
a = ['1,2,3,4', '1,2', '4,5,6', '']

result = [911, 912, 913, 914, 921, 922, 934, 935, 936, 94]

I tried this but still unable to get what I exactly want

result = [x for x in zip(c,a)]

Please help me.

Upvotes: 3

Views: 104

Answers (2)

Jasper
Jasper

Reputation: 3947

I tried to keep it readable:

C = [91.0, 92.0, 93.0, 94.0]
A = ['1,2,3,4', '1,2', '4,5,6', '']

result = []
for c, a in zip(C,A):
  str_c = str(int(c))
  nums = a.split(',')
  for num in nums:
    result.append(int(str_c + num))


print(result)

Upvotes: 1

sshashank124
sshashank124

Reputation: 32189

You can do it as follows:

c = [91.0, 92.0, 93.0, 94.0]
a = ['1,2,3,4', '1,2', '4,5,6', '']

c = map(str, map(int, c))

x = [int(c[k]+j) for k,i in enumerate(a) for j in i.split(',')]

>>> print x
[911, 912, 913, 914, 921, 922, 934, 935, 936, 94]

Upvotes: 4

Related Questions