Home > other >  Issue in creating list of tuple to insert bulk data in mysql using python
Issue in creating list of tuple to insert bulk data in mysql using python

Time:09-11

uniqueid='xyz1235'

list1=[1,2,3,4]
list2=['a','b','c','d']

Desire output should be :- [('xyz1235',1, 'a'), ('xyz1235',2, 'b'), ('xyz1235',3, 'c'), ('xyz1235',4, 'd')]

With the help of Zip i am able to do with list1 and list2 but not able to add uniqueid string in this way. Can you please help.

CodePudding user response:

There are various ways to achieve this.

One of them would be to use list comprehension to explicitly construct desired list:

out = [(uniqueid, item[0], item[1]) for item in zip(list1, list2)]

Another way is to forcefully build a list0 variable that you can then zip along with others, as in:

list0 = [uniqueid] * len(list1)
out = zip(list0, list1, list2)

Probably from there you can imagine your own creative ways of doing equivalent work.

  • Related