Problem Formulation and Solution Overview
π‘ Note: In theory, the dictionary data type doesn’t provide any guarantee of ordering. In other words, there should be no such thing as getting the “first value” of a dictionary. If your application relies on the ordering of dictionary elements, I’d recommend using the OrderedDict
type. In practice, however, things are different because dictionaries, in fact, keep the elements ordered in the cPython implementation of the Python specification. You cannot rely on it, though.
Noah has decided to take his family on a fun getaway. He has a list of five (5) possible choices. After much ado, a vote was taken, and the first location was selected as the winner. Where is the family going (Key)? What are they staying in (Value)?
Method 1: Use dict.items()
This method uses dict.items()
to access and retrieve the first key:value pair in a Dictionary.
getaways = {'Switzerland': 'Windmill', 'Canada': 'Igloo', 'Sweden': 'Treehouse', 'US': 'Lighthouse'} winner = list(getaways.items()) print(winner)
Above, declares four (4) possible getaways, with the Dictionary Key indicating the location and the Value indicating the accommodations. These save to getaways
.
The following line converts getaways
(list(getaways.items()
) to a List and returns a List of Tuples as shown below.
[('Switzerland', 'Windmill'), ('Canada', 'Igloo'), ('Sweden', 'Treehouse'), ('US', 'Lighthouse')] |
To access the first key:value pair, slicing
was used to access the Tuple
.
winner = list(getaways.items())[0] print(winner)
('Switzerland', 'Windmill') |
To access the Key and Value individually, slicing
was used once again.
winner = list(getaways.items())[0][0] print(winner) winner = list(getaways.items())[0][1] print(winner)
Switzerland |
Method 2: Use dict.keys() and dict.values()
This method uses dict.keys()
and dict.values()
to access and retrieve the first key:value pair in a Dictionary.
getaways = {'Switzerland': 'Windmill', 'Canada': 'Igloo', 'Sweden': 'Treehouse', 'US': 'Lighthouse'} first_key = list(getaways.keys())[0] first_val = list(getaways.values())[0] print(f'Noah and his family are going to {first_key} and staying in a {first_val}.')
Above, declares four (4) possible getaways, with the Dictionary Key indicating the location and the Value indicating the accommodations. These save to getaways
.
The following two (2) lines access the first Key and the first Value using slicing
. These save respectively to first_key
and first_val
.
Next, a formatted string is output to the terminal.
Noah and his family are going to Switzerland and staying in a Windmill. |
Method 3: Use next()
For this method, only the Value of the first key:value pair, is retrieved.
getaways = {'Switzerland': 'Windmill', 'Canada': 'Igloo', 'Sweden': 'Treehouse', 'US': 'Lighthouse'} all_vals = getaways.values() val_iter = iter(all_vals) first_val = next(val_iter) print(f'Noah and his family are staying in a {first_val}. The location is a secret!')
Above, declares four (4) possible getaways, with the Dictionary Key indicating the location and the Value indicating the accommodations. These save to getaways
.
The following three (3) lines retrieve all the Dictionary Values, convert them to an object, retrieve the first Value and output a formatted string to the terminal.
Noah and his family are staying in a Windmill. The location is a secret! |
Method 3: Use a For Loop and dict.items()
This method uses a for
Loop and dict.items()
to output the first key:value pair in a Dictionary.
getaways = {'Switzerland': 'Windmill', 'Canada': 'Igloo', 'Sweden': 'Treehouse', 'US': 'Lighthouse'} for k, v in getaways.items(): print(k, v) exit()
Above, declares four (4) possible getaways, with the Dictionary Key indicating the location and the Value indicating the accommodations. These save to getaways
.
The next line instantiates a For
loop to iterate through the Dictionary items.
Then, the first Key and Value are output to the terminal, and the loop terminates.
Switzerland Windmill |
Method 5: Use Dictionary Comprehension
Dictionary Comprehension
! A simplistic approach!
getaways = {'Switzerland': 'Windmill', 'Canada': 'Igloo', 'Sweden': 'Treehouse', 'US': 'Lighthouse'} winner = {k: getaways[k] for k in list(getaways)[:1]} print(winner)
Above, declares four (4) possible getaways, with the Dictionary Key indicating the location and the Value indicating the accommodations. These save to getaways
.
Next, Dictionary Comprehension
is used in conjunction with slicing
to retrieve only the first Key and Value in the Dictionary and output the same to the terminal. Perfect!
{'Switzerland': 'Windmill'} |
β¨A Finxter Favorite!
Simple! Yet Elegant!