0

I have a list of tuples that looks something like this:

list = [('spam', 9), ('the', 7), ('in', 6), ('my', 6), ('by', 5), ('and', 4)]

I want to turn each tuple into a string. I have some code that doesn't quite work...

for tup in list:
    tup = str(tup)
    print(tup)

and I'm a little lost. The code above just prints the same list I started with. Could someone help me out please? I'm new to python and programming in general so I'm sorry if this is a noob question.

4
  • 1
    What exactly do you want/expect? The default string representation of a tuple looks much like the python code that would create it. Commented Dec 11, 2013 at 22:17
  • print(tup) does the exact same thing as str(tup) and repr(tup). What output do you expect instead? Commented Dec 11, 2013 at 22:19
  • 2
    You shouldn't use variables like list that are the names of built-ins - you may get unexpected behavior later! Commented Dec 11, 2013 at 22:19
  • As a side note, this looks like data you'd want to store in a dict (or maybe a collections.Counter) rather than a list of tuples. It doesn't really affect this question (except that you'd need to add a .items() before the colon), but the rest of your code might well get a lot easier with that change. Commented Dec 11, 2013 at 22:35

4 Answers 4

3

You are doing exactly what you claim to want: turning each tuple into a string.

The problem is that the default string representation (in fact, both the human-friendly str and the programmer-friendly repr) looks exactly like the source code: converting ('spam', 9) to a string gives you the string "('spam', 9)".

If you want something different, you can do it. But you have to decide what you want before you can write the code for it. Then it's just a matter of using string formatting, or concatenation, or join.

For example, let's say you want it to print out this:

spam: 9
the: 7
in: 6
…

Then you can do this:

for tup in list:
    tup = '{}: {}'.format(*tup)
    print(tup)

Or, if you want this:

You have 9 copies of "spam".
You have 7 copies of "the".
…

Then it's this:

for tup in list:
    tup = 'You have {1} copies of "{0}"'.format(*tup)
    print(tup)

And so on.

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

5 Comments

Another possibility: only print one element from the tuple (probably the string value).
@MartijnPieters: or maybe print spam 9 times, then the 7 times, etc.?
Lots of possibilities at this point... even printing the strings in [1] numerical order, like "and by my in the spam"... heheh...
@abarnert: Exactly. Loads of options, and no word from the OP as to what is expected. I've VtCed as 'unclear what you are asking'.
@Roberto: No, obviously you're supposed to print the numbers as strings in order of the adler32 of the string encoded as EBCDIC. :)
1

You could try to use str.join() to join together the elements of each tuple. It would then require you to iterate over the contents of list and put each element into the str.join() function.

Comments

1

Are you looking for something like this?

for tup in my_list:
    tup = ' '.join(map(str,tup))
    print(tup)

Edit - just explaining a little bit:
' '.join(...) - this is the string .join method, it needs a joining string (' ' here) and an iterable as argument. The iterable contains strings.
map(str,tup) - the map() function takes a function name (in this case str) and an iterable, and applies the function to all members of the iterable. So it will make the tuple into a list of strings, ready for the .join method to work on. It is equivalent to the list comprehension [str(i) for i in tup].
So you could also do tup = ' '.join(str(i) for i in tup)

2 Comments

I doubt spam9 is the output he wants either… but maybe this is enough to show him that he has to decide what he wants before anyone can write the code that gives it to him…
I put a space as string, at least now it's spam 9! :)
1

Alternatively an one-liner. (Just as complement to the more elaborate answers!):

>>> '; '.join(map(lambda tpl: '%s: %s' % tpl, list))
'spam: 9; the: 7; in: 6; my: 6; by: 5; and: 4'

Taking Martijn Pieters comment into account:

>>> ' '.join(map(lambda tpl: '%s' % tpl[0], list))
'spam the in my by and'

2 Comments

I think that will be treated as unambiguous by both 2.x and 3.x, but really, but it's not ambiguous to a human reader! Just using a better name than i that makes it clear that each element is a tuple might be sufficient, but I might put either the lambda body or the whole lambda in parens…
@abarnert: roger that! ;-)

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.