Looping Through Keys Only
We can use the keys() method to display only the key values while omitting
the values contained in the dictionary. Let us use our election_list to
demonstrate this feat.
election_list = {
‘John Campbell’ : ‘Freedom Party,’
‘Eric Longman’: ‘Sovereign Nation Party,’
‘Amanda Drill’: ‘ Women Liberation
Movement,’
‘Condolence Rice’: ‘Democratic Party,’
‘Donald Trump’: ‘Republican Party,’
}
for name in election_list.keys():
print(name.title())
The line after the closing brace is the beginning of the “for loop,” which
tells the interpreter to pull only the keys from our election_list. Remember,
we assign names as the key. The output will be as follows:
John Campbell
Eric Longman
Amanda Drill
Condolence Rice
Donald Trump
Do'stlaringiz bilan baham: |