Python Program to Access Dictionary Items

Introduction

A dictionary in Python is a collection of key-value pairs, where each key is associated with a value. Accessing dictionary items involves retrieving the value associated with a specific key. This tutorial will guide you through creating a Python program that accesses items in a dictionary.

Example:

  • Dictionary: {'name': 'John', 'age': 25, 'city': 'New York'}
  • Accessing ‘name’: John
  • Accessing ‘age’: 25

Problem Statement

Create a Python program that:

  • Creates a dictionary with key-value pairs.
  • Accesses and displays the value associated with a specific key.
  • Handles cases where the key might not exist in the dictionary.

Solution Steps

  1. Create a Dictionary: Initialize a dictionary with some key-value pairs.
  2. Access a Dictionary Item: Use the key to access the value associated with it in the dictionary.
  3. Handle Non-Existent Keys: Use the get() method or exception handling to manage cases where the key does not exist.
  4. Display the Accessed Value: Use the print() function to display the value associated with the specified key.

Python Program

# Python Program to Access Dictionary Items
# Author: https://www.rameshfadatare.com/

# Step 1: Create a dictionary with key-value pairs
my_dict = {
    'name': 'John',
    'age': 25,
    'city': 'New York',
    'profession': 'Engineer'
}

# Step 2: Access dictionary items using keys
key_to_access = input("Enter the key you want to access: ")

# Step 3: Handle non-existent keys and access the value
value = my_dict.get(key_to_access, "Key not found in the dictionary.")

# Step 4: Display the accessed value
print(f"The value for '{key_to_access}' is: {value}")

Explanation

Step 1: Create a Dictionary with Key-Value Pairs

  • A dictionary my_dict is created with some key-value pairs such as 'name': 'John', 'age': 25, 'city': 'New York', and 'profession': 'Engineer'.

Step 2: Access Dictionary Items Using Keys

  • The input() function prompts the user to enter the key they want to access. This input is stored in the variable key_to_access.

Step 3: Handle Non-Existent Keys and Access the Value

  • The get() method is used to access the value associated with the key. If the key is not found, a default message "Key not found in the dictionary." is returned.

Step 4: Display the Accessed Value

  • The print() function is used to display the value associated with the specified key, or the default message if the key does not exist.

Output Example

Example 1:

Enter the key you want to access: name
The value for 'name' is: John

Example 2:

Enter the key you want to access: age
The value for 'age' is: 25

Example 3:

Enter the key you want to access: country
The value for 'country' is: Key not found in the dictionary.

Conclusion

This Python program demonstrates how to access items in a dictionary using keys. It also shows how to handle cases where the key might not exist using the get() method, making the program more robust and user-friendly. Understanding how to access dictionary items is crucial for effectively working with dictionaries in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top