You are currently viewing How to Access List Values within the Dictionary in Python

How to Access List Values within the Dictionary in Python

The dictionary is a data structure in Python that belongs to the mapping category. When data(key-value) is enclosed by curly({ }) braces, we can say it is a dictionary.

A dictionary has a key that holds a value, also known as a key-value pair. Using the dictionary[key], we can get the value assigned to the key.

What if the dictionary contains the values in the form of a list? In this article, we’ll look at all of the different ways to access items from lists within the dictionary.

Sample data

Given data contains items in a list as the values of keys within the dictionary. We’ll work with this given data throughout the article.

Sample data

Using Indexing

In the following example, we’ve used the indexing method and passed the index along with the key from which the value is to be extracted.

The convention dict_name[key][index] must be used to access the list values from the dictionary.

Output

When we accessed only the key, we got the entire list, but when we specified the index number with the key, we got the specific values.

Using slicing

It’s the same as the previous method, except instead of specifying the index value, we’ve used the slicing range.

Output

Using for loop

The for loop has been used in the following example to iterate over the values of the list in the dictionary my_data.

We iterated both keys and values from the dictionary my_data in the first block of code, then the list items from each key in the second block, the list items from each key within the nested dictionary detail in the third block, and the list items of the specific key in the last block of code.

Output

Using list comprehension

The list comprehension technique is used in the following example. It’s the same method as before, but this time we’ve used the for loop within the list.

Output

Using unpacking(*) operator

You’ve probably used the asterisk(*) operator for multiplication, exponentiation, **kwargs*args, and other things, but we’re going to use it as an unpacking operator.

The expression *my_data.values() in the preceding code will return all the values of the keys in the dictionary my_data, and once the values are returned, we can specify the index number to access specific data.

The expression *my_data.get('Python dev') returns the values of the key Python dev, and through indexing, we can access the list item.

Output

Conclusion

In this article, we’ve seen the different methods to access list items within the dictionary. The dictionary we’ve operated on contains the keys with the values as a list and also a nested dictionary.

We’ve used five methods to access the list items from the dictionary which are as follows:

  • Indexing – Using the bracket notation([ ])
  • Slicing – Using the list slicing
  • Iterating – Using the for loop
  • List comprehension technique
  • Unpacking(*) operator

Well, this is it for the article, try to find some more methods to access list items within the dictionary.


πŸ†Other articles you might be interested in if you liked this one

βœ…How list reverse() is different from the reversed() in Python?

βœ…8 different ways to reverse the Python list.

βœ…Python one-liners that will make your code more efficient.

βœ…Understanding NumPy argmax function in Python.

βœ…How to read multiple files simultaneously using the with statement in Python?

βœ…Using asynchronous(async/await) programming in Python.

βœ…Different ways to remove whitespaces in Python.


That’s all for now

Keep Coding✌✌