merging 2 lists as a key value pair in python

You can make l3 a dictionary, which stores key-value pairs:

>>> l3 = dict( zip(l1.split(','), l2.split(',')) )
>>> l3
{'brand': 'car', 'color': 'red', 'model': '2009', 'value': '100000'}

But if you just need a string, you can use join:

>>> l3 = ','.join([ '%s:%s' % (k, v) for k, v in zip(l1.split(','), l2.split(',')) ])
>>> l3
'model:2009,color:red,brand:car,value:100000'

Note that l1 and l2 are not lists, but strings. Thus we can convert them to lists by splitting on the commas, e.g. l1.split(',').

Browse More Popular Posts

Leave a Comment