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.
print(tup)does the exact same thing asstr(tup)andrepr(tup). What output do you expect instead?listthat are the names of built-ins - you may get unexpected behavior later!dict(or maybe acollections.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.