Reputation: 3
The input:
z = ", "
y = " "
chands = {'theboard': [('Diamonds', 'Four'), ('Clubs', 'Three'), ('Clubs', 'King'), ('Clubs', 'Two'), ('Hearts', 'Jack')]}
My expected output:
'Diamonds Four, Clubs Three, Clubs King, Clubs Two, Hearts Jack'
I've tried:
print chands["theboard"][0][0]+y+chands["theboard"][0][1]+z+chands["theboard"][1][0]+y+chands["theboard"][1][1]+z+chands["theboard"][2][0]+y+chands["theboard"][2][1]
Is there a better way to print this?
Upvotes: 0
Views: 51
Reputation: 402513
IIUC, use map
+ str.join
, once on y
and once more on z
-
>>> z.join(map(y.join, chands['theboard']))
'Diamonds Four, Clubs Three, Clubs King, Clubs Two, Hearts Jack'
If you want to join just the first 3 tuples, you can index chands
and slice the result -
>>> z.join(map(y.join, chands['theboard'][:3]))
'Diamonds Four, Clubs Three, Clubs King'
Another way to do this with a list comprehension would be -
>>> z.join([y.join(x) for x in chands['theboard'][:3]])
'Diamonds Four, Clubs Three, Clubs King'
Upvotes: 1