Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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')]
Add a comment
You could use zip() and itertools.repeat():
zip()
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']))
Required, but never shown
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.
Explore related questions
See similar questions with these tags.