0

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.

3 Answers 3

4
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')]
2

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')]
1

Another variation...

list1 = ['car', 'bike', 'mango'] 
from itertools import product

list2 = list(product(list1, ['JNU']))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.