1

is there a way that i can concatenate a string list and 2 number list such that the concatenated list would take them one after the order while joining them.

Input:

a = [john, bob, ted, Paul]
b = [22, 34, 56, 12]
c = [13, 98, 78, 60]

Expected Ouput:

[john, 22, 13, bob, 34, 98, ted, 56, 78, Paul, 12, 60]
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
Joel Stephen
  • 21
  • 2
  • 7
  • 3
    Possible duplicate of [Interleaving multiple lists in Python](https://stackoverflow.com/questions/7946798/interleaving-multiple-lists-in-python) – Dani Mesejo Sep 28 '18 at 02:37
  • if you don't care about the order of the list you can also add the list together like `a + b + c` – badger0053 Sep 28 '18 at 02:50

3 Answers3

2

You can use the zip function with list comprehension:

a = ['john', 'bob', 'ted', 'Paul']
b = [22, 34, 56, 12]
c = [13, 98, 78, 60]
outcome = [i for t in zip(a, b, c) for i in t]

outcome would become:

['john', 22, 13, 'bob', 34, 98, 'ted', 56, 78, 'Paul', 12, 60]

Note that the c list in your question has an extra 90 that is not in your expected output, so I removed it believing it's a typo on your part.

blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Using formatted string and enumerate with list comprehension

l = ['{}, {}, {}'.format(a[idx], b[idx], c[idx]) for idx, item in enumerate(a)]
['john, 22, 13', 'bob, 34, 98', 'ted, 56, 78', 'Paul, 12, 60']
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

Your lists : Make Sure all lists have same dimensions.

a = ['john', 'bob', 'ted', 'Paul']
b = [22, 34, 56, 12]
c = [13, 98, 78, 90]

Import numpy a great module for handling arrays

import numpy as np

Make numpy array and reshape them as you want

a = np.array(a).reshape(-1,1)
b = np.array(b).reshape(-1,1)
c = np.array(c).reshape(-1,1)

Concate them along the required axis. In your case it will be 1 (Column wise as we have reshaped the arrays)

f= np.concatenate((a,b,c),axis=1)

Flatten them in 1D array

f = f.flatten()
print(f)
LMSharma
  • 279
  • 3
  • 10