how to get key value pairs from dictionary in python

Extract Key-Value Pairs from Python Dictionary: A Step-by-Step Guide

By Robert White
AD

Dictionaries in Python, with their key-value pair structure, are incredibly useful for storing and manipulating data. When you need to extract these key-value pairs, however, you might find yourself scratching your head. This article guides you through several methods to get key-value pairs from a dictionary in Python, ensuring your code is efficient and intuitive.

Before we delve into the methods, let's understand the basic syntax of a Python dictionary. A dictionary is defined using curly braces {} where keys and values are separated by a colon :. For example, my_dict = {'name': 'John', 'age': 30}.

Using Items Method

The items() method returns a view object that displays a list of (key, value) tuple pairs.

AD
Python Dictionary Complete Guide Key-Value Pairs, Methods & Real-World Use Cases
Python Dictionary Complete Guide Key-Value Pairs, Methods & Real-World Use Cases
πŸ“š Python Dictionaries: Working with Key-Value Pairs πŸ“š
πŸ“š Python Dictionaries: Working with Key-Value Pairs πŸ“š
a notebook with the words'15 python dictionary'written on it
a notebook with the words'15 python dictionary'written on it

Here's how you can use it:

Example 1: Iterating through items

Imagine you have a dictionary student = {'name': 'Alice', 'age': 20, 'grade': 'A'}. To get all key-value pairs, you can use the following:

CBSE Class 11 Dictionary Notes πŸ“–
CBSE Class 11 Dictionary Notes πŸ“–
Python Dictionaries Explained (With Real Examples)
Python Dictionaries Explained (With Real Examples)
Python Dictionaries – Beginner Cheat Sheet | Python Basics #11
Python Dictionaries – Beginner Cheat Sheet | Python Basics #11
for key, value in student.items():
    print(f'{key}: {value}')

The output will be:

name: Alice
age: 20
grade: A

Example 2: Storing items in a new dictionary

You can also create a new dictionary using the items() method. Suppose you want to create a new dictionary where all values are doubled. Here's how to do it:

AD
student_double = {key: value * 2 for key, value in student.items()}

The result will be {'name': 'AliceAlice', 'age': 40, 'grade': 'AA'}.

Python Sort Dictionary by Key and Value with Example
Python Sort Dictionary by Key and Value with Example
Python Dictionaries Explained in the Simplest Way Possible
Python Dictionaries Explained in the Simplest Way Possible
10. Dictionaries
10. Dictionaries
Python Dictionary Cheat Sheet 🐍 | Quick & Easy Python Notes
Python Dictionary Cheat Sheet 🐍 | Quick & Easy Python Notes
a screenshot of the 3 key value pairs in an image with text that reads'3 key value pairs each item in a dictionary has a key and a key and a
a screenshot of the 3 key value pairs in an image with text that reads'3 key value pairs each item in a dictionary has a key and a key and a
Python notes 🐍
Python notes 🐍

Using Dictionary Comprehension

Dictionary comprehension offers a concise and efficient way to create dictionaries based on existing ones. You can use it to extract key-value pairs, similar to the items() method.

Example 3: Filtering key-value pairs

Let's say you want to create a new dictionary that only includes key-value pairs where the key starts with 'a'. Here's how:

AD
new_dict = {key: value for key, value in student.items() if key.startswith('a')}

The result will be an empty dictionary because no keys in student starts with 'a'.

Python dictionaries cheat-sheet infographic
Python dictionaries cheat-sheet infographic
the words dictionary are written in different languages
the words dictionary are written in different languages
Python Lists vs Tuples vs Sets vs Dictionaries: Easy Beginner Cheat Sheet
Python Lists vs Tuples vs Sets vs Dictionaries: Easy Beginner Cheat Sheet

Example 4: Modifying key or value

You can also modify the keys or values while creating a new dictionary. For instance, you can convert all keys to uppercase:

new_dict = {key.upper(): value for key, value in student.items()}

The result will be {'NAME': 'Alice', 'AGE': 20, 'GRADE': 'A'}.

AD

Incorporating these methods into your Python workflow will greatly enhance your productivity and understanding of dictionaries. Keep practicing, keep exploring, and happy coding!

AD