Jim Lewis
Jim Lewis

Reputation: 349

How to create a string from elements in tuples in a list

Is there a more succinct way to do this?

MyList = [(1,2,3),(6,6,7)] # example
msg = "";
for (a,b,c) in MyList:
   msg += str(b)

Upvotes: 0

Views: 37

Answers (1)

Thomas Kimber
Thomas Kimber

Reputation: 11067

Yes, join.

str_msg = "".join([str(b) for (a,b,c) in MyList])

Where the starting quotes contain any delimiter you might want to use between elements and where MyList is some collection of string items.

Upvotes: 3

Related Questions