my input is:
list1=['car','bike','mango']
and I want to append "JNU" to every item. Desired output:
list1=[('car', 'JNU'), ('bike', 'JNU'), ('mango', 'JNU')]
I'm unable to get that result.
In [13]: list1 = ['car', 'bike', 'mango']
In [14]: list1 = [(el, 'JNU') for el in list1]
In [15]: list1
Out[15]: [('car', 'JNU'), ('bike', 'JNU'), ('mango', 'JNU')]
You could use zip()
and itertools.repeat()
:
import itertools
list1 = zip(list1, itertools.repeat('JNU'))
Demo:
>>> import itertools
>>> list1 = ['car','bike','mango']
>>> zip(list1, itertools.repeat('JNU'))
[('car', 'JNU'), ('bike', 'JNU'), ('mango', 'JNU')]
Another variation...
list1 = ['car', 'bike', 'mango']
from itertools import product
list2 = list(product(list1, ['JNU']))