Unpacking a list in Python


Python tip:

You can unpack list elements to variables. You can also ignore some of the elements.

👇

tournament_results = ["Jan", "Mike", "Marry", "Bob"]

first_player, *_, last_player = tournament_results
print(first_player, last_player)
# => Jan Bob

*_, last_player = tournament_results
print(last_player)
# => Bob

first_player, *_ = tournament_results
print(first_player)
# => Jan

first_player, second_player, third_player, fourth_player = tournament_results
print(first_player, second_player, third_player, fourth_player)
# => Jan Mike Marry Bob