Key Index in Python Dictionary

In this Python tutorial, we will discuss the Python Dictionary index. Here we will also cover some examples.

To find the index of the values in the dictionary, follow the below 2 methods.

  • Using keys() + list()+ index()
  • Using list comprehension & enumerate()

Key Index in Python Dictionary Using keys() + list()+ index()

Python dictionaries are data structures that store key-value pairs, allowing you to efficiently access information using a unique identifier, called a key.

We’ll discuss how to use the built-in list(), keys(), and index() functions with dictionaries.

Example-1: Using list() function

  • list() function: To convert a dictionary into a list of keys, you can use the keys() method:
#creates a dictionary called 'country' with key-value pairs of country codes and country names.

country = {'1': 'USA', '2': 'United Kingdom'}

#The 'keys()' method is then used to get a list of all the keys in the dictionary, which are the country codes.
#The 'list()' function is used to convert the returned keys object into a list.
keys = list(country.keys())

#Finally, the 'print()' function is used to display the list of keys (country codes) on the console.
print(keys)

The above code creates a dictionary called “country” with key-value pairs of country codes and country names.

  • The dictionary has two keys: “1” and “2”, which map to the values “USA” and “United Kingdom”, respectively.
  • The “keys()” method is then used to get a list of all the keys in the “country” dictionary, which are the country codes. The “keys()” method returns a list-like object containing the keys of the dictionary.
  • The “list()” function is then used to convert this list-like object into a real list. This list is stored in the variable “keys”.
  • Finally, the “print()” function is used to display the “keys” list
Python Dictionary index using list
Python Dictionary index using list

Read How to convert dictionary to JSON in Python

READ:  Multiply in Python with Examples

Example-2: Using the keys()

  • keys() method: This method returns a list of all the keys in the dictionary. This can be useful when you need to iterate over the keys in a dictionary:
#creates a list of keys from the dictionary 'country' using the 'keys()' method.
keys = country.keys()
#The 'for' loop then iterates over the list of keys.
for key in keys:
  #The 'print()' function is used to display the key and value (country code and country name)
  print(key, country[key])

The above code defines a dictionary named “country” which maps country codes to their names.

  • The first line of code creates a list of keys from the “country” dictionary using the “keys()” method. The “keys()” method returns a list-like object containing the keys of the dictionary. This list-like object is stored in the variable “keys”.
  • The next section of code uses a “for” loop to iterate over the “keys” list. For each iteration, the “key” variable is assigned the current key in the “keys” list.
  • In the “for” loop, the “print()” function is used to display the key and its corresponding value (country code and country name) from the “country” dictionary. The value is retrieved using the key as the index in square brackets, e.g. country[key].
  • The output of this code will be a list of the keys (country codes) and their corresponding values (country names).
Python Dictionary index using keys
Python Dictionary index using keys

Read Python dictionary append with examples

Example-3: Using the index()

  • index() function: This function is used to find the index of an item in a list. Since dictionaries do not have a sequential index.

You cannot use the index() function directly with dictionaries. However, you can use it with the list of keys or values obtained using the list() and keys()/values() methods.

#Converts the keys of the dictionary 'country' into a list using the 'list()' function and the 'keys()' method.
keys = list(country.keys())
#The 'index()' function is then used to find the index of the key '1' in the list of keys.
index = keys.index('1')
#The 'print()' function is used to display the index of the key '1' on the console.
print(index)

The above code defines a dictionary named “country” which maps country codes to their names.

  • The first line of the code converts the keys of the “country” dictionary into a list using the “list()” function and the “keys()” method. The “keys()” method returns a list-like object containing the keys of the dictionary.
  • The “list()” function then converts this list-like object into a real list. This list is stored in the variable “keys”.
  • The next line of code uses the “index()” function to find the index of the key “1” in the “keys” list. The “index()” function returns the first index at which the specified value appears in the list. If the specified value is not found, it raises a ValueError exception.
  • Finally, the “print()” function is used to display the index of the key “1”.
Python Dictionary index using index
Python Dictionary index using index

Read Python Concatenate Dictionary + Examples

READ:  How to find the size of a Python list

Key Index in Python Dictionary Using list comprehension and enumerate()

Use the list comprehension and enumerate() to get the key and index of items in a dictionary.

# create a dictionary with keys as numbers and values as countries
country = {'1': 'USA', '2': 'United Kingdom','3': 'Asia'}

# use list comprehension and enumerate() to create a list of tuples, 
# where each tuple contains the key and index of an item in the dictionary
result = [(key, index) for index, key in enumerate(country.keys())]

# print the list of tuples
print(result)

The above code defines a dictionary named “country” which maps country codes to their names.

  • Then, it creates a new list “result” using a list comprehension. The list comprehension iterates over the items of the “country.keys()” object, which returns a list of the keys in the “country” dictionary.
  • For each iteration of the list comprehension, the key and its index are packed into a tuple and appended to the “result” list.
  • Finally, the “result” list is printed to the console using the “print” function.
Python Dictionary index using list comprehension and enumerate
Python Dictionary index using list comprehension and enumerate

In this Python tutorial, we will discuss the Python Dictionary index by following the below methods.

  • Using keys() + list()+ index()
  • Using list comprehension and enumerate()

You may also like to read the following Python tutorials.