46

I am trying to get this string as a result:

"&markers=97,64&markers=45,84"

From the Python code below:

markers = [(97,64),(45,84)]
result = ("&markers=%s" %x for x in markers)
return result

How do I do this as the below does not give me the actual string?

6 Answers 6

49

You need to join your string like this:

markers = [(97,64),(45,84)]
result = ''.join("&markers=%s" % ','.join(map(str, x)) for x in markers)
return result

UPDATE

I didn't initially have the ','.join(map(str, x)) section in there to turn each tuple into strings. This handles varying length tuples, but if you will always have exactly 2 numbers, you might see gatto's comment below.

The explanation of what's going on is that we make a list with one item for each tuple from markers, turning the tuples into comma separated strings which we format into the &markers= string. This list of strings is then joined together separated by an empty string.

Sign up to request clarification or add additional context in comments.

3 Comments

TypeError: not all arguments converted during string formatting
it complained it wasnt a string so i did this which also returns a generator object, not the actual string result = "".join(str(("&marker=%s" % x for x in markers)))
@gatto, unless the tuples are of variable length. see my updated answer
18

In Python 3.6 you could write:

markers = [(97,64),(45,84)]
result = ''.join(f'&markers={pair}' for pair in markers)
return result

Comments

11

While the first answer is doing what's expected, I'd make it a bit more "pythonic" by getting rid of map and nested expressions:

def join(seq, sep=','):
    return sep.join(str(i) for i in seq)

result = ''.join('&markers=%s' % join(m) for m in markers)

(if that's for urls like it seems, you can also take a look at urllib.urlencode)

3 Comments

Not quite. Your join function will never get called. Should be without the ''.: result = join('&markers=%s' % join(m) for m in markers)
@modle13 my join is called for each element in the loop.
This is really confusing because you're creating a new function with the same name as the built-in string method "join".
6

Here's another approach that hopefully makes the intent the most clear by specifying the location of each of your values explicitly:

markers = [(97,64),(45,84)]
print ''.join('&markers=%s,%s' % pair for pair in markers)

Comments

4

Try creating an empty string adding to it then removing the last comma

result = ''

for i in a:
    result+='&markers'
    for j in i:
    result += str(j) + ','
result = result[:-1]

return result

Comments

0

Another way is using f-string and list comprehension, both readable and understandable:

markers = [(97,64),(45,84)]

result = ''.join([f'&markers={value[0]},{value[1]}' for value in markers])

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.