Problem Formulation and Solution Overview
β₯οΈ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
Method 1: Use Dictionary and List
These two options in this method convert a Dictionary into a List format to extract the keys. Then the appropriate key is accessed based on the scenario above.
staff = {'Amy': 23, 'Ben': 32, 'Micah': 37, 'Jon': 19, 'Karn': 39}
names = list(staff)
print(names[3])This code declares a Dictionary containing five (5) key:value pairs and saves them to staff.
Option 1 above converts staff into a List format extracting the keys and saving them to names. The content of the appropriate key ([3]) is output to the terminal.
OR
staff = {'Amy': 23, 'Ben': 32, 'Micah': 37, 'Jon': 19, 'Karn': 39}
print(list(staff)[3])Option 2 above extracts the keys from staff and converts them into a List format referencing the appropriate key ([3]) and sending the output to the terminal.
π‘Note: Both options produce the same results. However, Option 2 is more compact.
Output
| Jon |
Method 2: Use List and dict.items()
This example converts a Dictionary into a List format, then extracts and returns the key:value pairs as a List of Tuples.
staff = {'Amy': 23, 'Ben': 32, 'Micah': 37, 'Jon': 19, 'Karn': 39}
names = list(staff.items())
print(names[3][0])This code declares a Dictionary containing five (5) key:value pairs and saves them to staff. Then staff.items() is called to extract these pairs as a List of Tuples.
If we output print(names) to the terminal, the following List of Tuples displays.
[('Amy', 23), ('Ben', 32), ('Micah', 37), ('Jon', 19), ('Karn', 39)] |
If we output (print(names[3])) to the terminal, the referenced Tuple from the List displays.
('Jon', 19) |
Then, to retrieve the first element of this Tuple, [0] is appended to names (print(names[3][0])). Finally, this result is output to the terminal.
Output
Jon |
Method 3: Use List Comprehension and dict.items()
This example uses List Comprehension and dict.items() to look for and return the appropriate key based on a value.
staff = {'Amy': 23, 'Ben': 32, 'Micah': 37, 'Jon': 19, 'Karn': 39}
name = [k for k, v in staff.items() if v == 19]
print(name)This code declares a Dictionary containing five (5) key:value pairs and saves them to staff. List Comprehension used with dict.items() searches for and returns the appropriate key where its associated value equals 19.
The result outputs to the terminal.
Output
['Jon'] |
To remove the surrounding brackets and quotes, append [0] to the end of the List Comprehension statement to produce the desired result.
staff = {'Amy': 23, 'Ben': 32, 'Micah': 37, 'Jon': 19, 'Karn': 39}
name = [k for k, v in staff.items() if v == 19][0]
print(name)Output
| Jon |
Method 4: Use Dictionary Comprehension and dict.items()
This example uses Dictionary Comprehension and dict.items() to look for and return the appropriate key based on a value.
staff = {'Amy': 23, 'Ben': 32, 'Micah': 37, 'Jon': 19, 'Karn': 39}
name = {k for k, v in staff.items() if v == 19}
print(list(name)[0])This code declares a Dictionary containing five (5) key:value pairs and saves them to staff. Dictionary Comprehension with dict.items() searches for and returns the appropriate key where its associated value equals 19.
The result outputs to the terminal.
As shown above, if [0] was not appended to name in the print statement, (print(list(name))) the following output displays.
Output
['Jon'] |
To remove the surrounding brackets and quotes, append [0] to the print statement (print(list(name)[0])) to produce the desired result.
| Jon |
Summary
These five (4) methods of accessing a Dictionary Key by Index should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!