How do I use zip print on a list of strings in python without it returning with quotations surrounding them?

Question:

Current code:

reps = ['x 5', 'x 5', 'x 3', 'x 5', 'x 5', 'x 5+']
b = [90, 122, 135, 146, 168, 191]

print(str(list(zip(b,reps))).replace(',',''))

here is the current output:

[(90 'x 5') (112 'x 5') (135 'x 3') (146 'x 5') (168 'x 5') (191 'x 5+')]

here is my goal output:

[(90 x 5) (112 x 5) (135 x 3) (146 x 5) (168 x 5) (191 x 5+)]

How would I remove those single quotes?

I tried using replace but I’m not sure how to both replace the commas separating the weight and the reps, as well as the quotations around the rep range.

I also tried replacing the quotations in the list reps on a separate line of code but once it is printed within the zip they were added back.

Asked By: Lowsaint 98

||

Answers:

Not a thing of beauty, but if your only goal is to end up with exactly the output you specified then this will do that:

reps = ['x 5', 'x 5', 'x 3', 'x 5', 'x 5', 'x 5+'] 
b = [90, 122, 135, 146, 168, 191]

# creates a list of strings of the form: "(X x Y)"
arr = [f"({y} {x})" for x, y in zip(reps, b)]

# prints the list as a single string surrounded by brackets
print(f"[{' '.join(arr)}]")

Output:

[(90 x 5) (122 x 5) (135 x 3) (146 x 5) (168 x 5) (191 x 5+)]
Answered By: rhurwitz

It sounds like cosmetic output, but the following will work for your case:

print('[' + ' '.join(f"({x} {y})" for x, y in zip(b, reps)) + ']')

[(90 x 5) (122 x 5) (135 x 3) (146 x 5) (168 x 5) (191 x 5+)]
Answered By: RomanPerekhrest

if you want that exact format you can use

print(
    str(list(map(lambda tup: f"({str(tup[0])} {tup[1]})",list(zip(b,reps))))).replace("'",'').replace(",",'')
)

print(str([f"({x} {y})" for x, y in zip(b, reps)]).replace(",", "").replace("'",""))

Maybe is not the most efficient way, but it works

Answered By: Mauxricio
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.