7. Becoming Pythonic
Activity 18: Building a Chess Tournament
Solution:
- Open the Jupyter Notebook.
- Define the list of player names in Python:
names = ["Magnus Carlsen", "Fabiano Caruana", "Yifan Hou", "Wenjun Ju"]
- The list comprehension uses the list of names twice because each person can either be player 1 or player 2 in a match (that is, they can play with the white or the black pieces). Because we don't want the same person to play both sides in a match, add an
if
clause that filters out the situation where the same name appears in both elements of the comprehension:fixtures = [f"{p1} vs. {p2}" for p1 in names for p2 in names if p1 != p2]
- Finally, print the resulting list so that the match officials can see who will be playing whom:
print(fixtures)
You should get the following output:
In this activity, we...