yearntolearn
yearntolearn

Reputation: 1074

Making pairs of a string and an element in a list

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

Answers (3)

ospahiu
ospahiu

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

citaret
citaret

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

pretzlstyle
pretzlstyle

Reputation: 2962

How about a list comprehension?

str = 'era'
list = ['we', 'st']
packed = [(str,str2) for str2 in list]

Upvotes: 7

Related Questions