Python List of tuples positionally

2 lists of tuples are like this,

a=[(id,     name, value_1, value_2),(id,    name, value_3, value_4)..]
b=[(id_key, name, value_5, value_6),(id_key,name, value_7, value_8)..]

I want to merge it like this,

c=[(id,'', name, value_1, value_2),
   (id,'', name, value_3, value_4),
   ..
   ..
   ('', id_key,name , value_5, value_6),
   ('', id_key,name , value_7, value_8)
   ..
]

So, second list will concatenated to first list with members shifted.

Whats the best way to do this?

Answers


Use two list comprehensions to build new tuples from the old:

[(t[0], '') + t[1:] for t in a] + [('',) + t for t in b]

Demo:

>>> a=[('id',     'name', 'value_1', 'value_2'),('id',    'name', 'value_3', 'value_4')]
>>> b=[('id_key', 'name', 'value_5', 'value_6'),('id_key','name', 'value_7', 'value_8')]
>>> [(t[0], '') + t[1:] for t in a] + [('',) + t for t in b]
[('id', '', 'name', 'value_1', 'value_2'), ('id', '', 'name', 'value_3', 'value_4'), ('', 'id_key', 'name', 'value_5', 'value_6'), ('', 'id_key', 'name', 'value_7', 'value_8')]

Need Your Help

Why would IDirect3DDevice9::Release return S_FALSE?

com directx direct3d directx-9 direct3d9

When I call the Release method for my Direct3D9 device it returns S_FALSE but I can't find anything that explains why this value would be returned.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.