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')]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

Another variation...

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.