Accessing items in lists within a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Accessing items in lists within a dictionary in Python #

To access items in lists within a dictionary:

  1. Use bracket notation to access a specific key in the dictionary.
  2. Use bracket notation to access an index in the list.
  3. The expression returns the list item at the specified index.
main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } print(my_dict['names'][0]) # 👉️ alice print(my_dict['names'][1]) # 👉️ bobbyhadz print(my_dict['tasks'][-1]) # 👉️ ship for key, value in my_dict.items(): print(key, value) # 👇️ check if value in list if 'bobbyhadz' in value: print(key, value)

The first set of square brackets is used to access a specific key in the dictionary and returns the corresponding list value.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } print(my_dict['names']) # 👉️ ['alice', 'bobbyhadz', 'carl'] print(my_dict['tasks']) # 👉️ ['develop', 'test', 'ship'] print(my_dict['names'][0]) # 👉️ alice print(my_dict['names'][1]) # 👉️ bobbyhadz print(my_dict['tasks'][-1]) # 👉️ ship

The second set of square brackets is used to access an item in the list.

You can also use list slicing to access items in the lists.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } print(my_dict['names'][0:2]) # 👉️ ['alice', 'bobbyhadz']

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

If you need to check if a specific value is contained in any of the lists, use a for loop to iterate over the dictionary's items.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } for key, value in my_dict.items(): print(key, value) # 👇️ check if value in list if 'bobbyhadz' in value: print(key, value)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } # 👇️ dict_items([('names', ['alice', 'bobbyhadz', 'carl']), ('tasks', ['develop', 'test', 'ship']), ('ids', [1, 2, 3])]) print(my_dict.items())
On each iteration, we use the in operator to check if a value is contained in each list and print the matching key and value.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

x not in s returns the negation of x in s.

If you only need access to the lists when iterating, use the dict.values() method instead.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } for value in my_dict.values(): # 👇️ check if value in list if 'bobbyhadz' in value: # 👇️ ['alice', 'bobbyhadz', 'carl'] print(value)

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'ids': [1, 2, 3], } # 👇️ dict_values([['alice', 'bobbyhadz', 'carl'], ['develop', 'test', 'ship'], [1, 2, 3]]) print(my_dict.values())
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.