You're not too terribly far off for what you're trying, but I don't think you're understanding all the little pieces that are working together to make your code samples do what they're supposed to do, and there's a few little mistakes (syntax errors) because of this.
For example, your original list of dictionaries should be as follows:
>>> players_list = [
... {'name': 'Tom', 'points': 2},
... {'name': 'Jerry', 'points': 3},
... {'name': 'Paul', 'points': 4}
... ]
Notice the additional quotes around the strings denoting the names of the players. You can then work with this list using Python's fantastic list comprehensions.
>>> [player['name'] for player in players_list]
['Tom', 'Jerry', 'Paul']
>>> [(player['name'], player['points']) for player in players_list]
[('Tom', 2), ('Jerry', 3), ('Paul', 4)]
Although that latter is probably easier to think of as an expression involving a dict method:
>>> list(tuple(player.values()) for player in players_list)
[('Tom', 2), ('Jerry', 3), ('Paul', 4)]
So if you want to print out your first example, "Scores: [name]([points]), [name]([points]), ...", first you need to come up with the list above to feed to your format string. In fact, you can apply the format right in the list comprehension:
>>> ['%s(%s)' % tuple(player.values()) for player in players_list]
['Tom(2)', 'Jerry(3)', 'Paul(4)']
Now you have almost what you want, you just want the title "Scores: " in front and a comma separated string instead of a Python list, hence:
>>> print('Scores: %s' % ', '.join(_))
Scores: Tom(2), Jerry(3), Paul(4)
...where _
is the typical variable in the Python REPL which stores your last result. In other words, all at once:
>>> print('Scores: %s' % ', '.join(list(tuple(player.values()) for player in players_list)))
Scores: Tom(2), Jerry(3), Paul(4)
If you break it down into smaller steps and put it together like this, I think it's easier to see what to do. As far as what join
does, it's very simple: it writes a list (an iterable) as a string "joined" by some delimiter. In this case, once you get the list into a suitable format, join's purpose is to write it out as a comma-separated string, by adding ', '
between all the elements.
Applying this logic it should be simpler to get your second example to work properly.
As noted in the comments I got a little lucky here in terms of sorting. In one example, I pointed out the equivalence of player.values()
if you really are interested in all the values, so you don't have to name them one by one. However, since dict
s in Python are unsorted, sometimes this reverses the point score with the player's name, resulting in:
>>> list(tuple(player.values()) for player in players_list)
[(2, 'Tom'), (3, 'Jerry'), (4, 'Paul')]
For longer dicts, the way to get around this would be to explicitly sort the keys the way you want. Here, though, assuming in the real code there are still only two keys, it is more efficient and readable to 'hard-code' and just call the keys you want by name:
>>> [(player['name'], player['points']) for player in players_list]
[('Tom', 2), ('Jerry', 3), ('Paul', 4)]
>>> print('Scores: %s' % ', '.join([(player['name'], player['points']) for player in players_list]))
Scores: Tom(2), Jerry(3), Paul(4)