Reputation: 119
I have this list once merged using the below gives me this output.
['description t2_HELP', 'description t2_1507', 'description t2_1189', 'description t2_5625', 'description None', 'description None', 'description None', 'description None', 'interface Gi3/0/13', 'interface Gi3/0/7', 'interface Gi1/0/11', 'interface Gi3/0/41']
I however want to maintain the order of that data when I merge it in. So example list set would look like this.
['interface Gi3/0/25','description None','description t2_2696','interface Gi1/0/29','description None','description t2_4148','interface Gi1/0/31','description None','description t2_4212','interface Gi2/0/31','description None','description t2_4271']
Here is how i'm merging the lists,
joinlist = data1 + data2 + data3
the lists look something like this
data1 = ['interface Gi3/0/25','interface Gi1/0/29','interface Gi1/0/31','interface Gi2/0/31']
data2 = ['description None','description None','description None','description None']
data3 = ['description t2_2696','description t2_4148','description t2_4212','description t2_4271']
Upvotes: 2
Views: 78
Reputation: 155403
The other suggestions are fine if you have equal length list
s (zip
) or don't mind filler values in the result (zip_longest
).
But logically what you're asking for here is a round robining of inputs, not a zipping. By flattening, you lose the paired-up aspect of zip
, and can get bitten by it for uneven length inputs.
A more general solution to this problem is available with the itertools
module's roundrobin
recipe (click the link for code, I'm just including usage here):
joinlist = list(roundrobin(data1, data2, data3))
That takes an element from each of the inputs in order; if one input is shorter than the others, it is omitted from later rounds without injecting garbage filler values or losing values from the longer input(s).
Upvotes: 1
Reputation: 59184
You can use a combination of list comprehensions and zip
:
>>> [i for t in zip(data1, data2, data3) for i in t]
['interface Gi3/0/25', 'description None', 'description t2_2696', 'interface Gi1/0/29', 'description None', 'description t2_4148', 'interface Gi1/0/31', 'description None', 'description t2_4212', 'interface Gi2/0/31', 'description None', 'description t2_4271']
Upvotes: 0
Reputation: 29742
Use built-in zip
if you are okay with any longer list get truncated:
joinlist = []
for d in zip(data1,data2,data3):
joinlist.extend(d)
Output:
['interface Gi3/0/25',
'description None',
'description t2_2696',
'interface Gi1/0/29',
'description None',
'description t2_4148',
'interface Gi1/0/31',
'description None',
'description t2_4212',
'interface Gi2/0/31',
'description None',
'description t2_4271']
If you want to avoid the truncate, use itertools.zip_longest
:
data2.append('test_me') # For testing purpose
joinlist = []
for d in zip_longest(data1,data2,data3):
joinlist.extend(d)
Output:
['interface Gi3/0/25',
'description None',
'description t2_2696',
'interface Gi1/0/29',
'description None',
'description t2_4148',
'interface Gi1/0/31',
'description None',
'description t2_4212',
'interface Gi2/0/31',
'description None',
'description t2_4271',
None,
'a',
None]
Note: None
at the end of the joinlist
is because the zip_longest
, by default, zips shorter iterable(s) with None
. You can set any default value using fillvalue
of zip_longest
.
Upvotes: 1