Reputation: 1074
I have a single string that I would like to make pair with each element in a list.
str = "era"
list = ["we", "st"]
With the code:
zip(str, list)
I obtain
[('e', 'we'), ('r', 'st')]
What I would like to achieve is a list of tuples of the pairs:
[('era', 'we'), ('era', 'st')]
Is there a simple way to get around with the split of the string? Thank you!!
Upvotes: 0
Views: 607
Reputation: 3525
Thought I'd offer an alternative to @jphollowed's simple and concise answer.
s = "era"
l = ["we", "st"]
print([tuple(i.split(',')) for i in map(lambda x: x+','+s, l)])
Upvotes: 0
Reputation: 446
Use itertools.product
:
import itertools
ss = "era"
lst = ["we", "st"]
print list(itertools.product([ss], lst))
Avoid using keyword as variables, such as list.
Upvotes: 2
Reputation: 2962
How about a list comprehension?
str = 'era'
list = ['we', 'st']
packed = [(str,str2) for str2 in list]
Upvotes: 7