DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 55 of 77
←PreviousPrevNextNext→

Access Dictionary Items

Accessing Values by Key

Dictionary values can be retrieved by providing their key inside square brackets. If the key does not exist, this raises a KeyError. To avoid errors, you can use the get() method, which returns None (or a default value if specified) when the key is missing.

Example
# Accessing dictionary values
person = {"name": "John", "age": 30, "city": "New York"}

# Using square brackets (raises error if key not found)
print(person["name"])

# Using get() method (safer)
print(person.get("age"))
print(person.get("country"))  # Returns None
print(person.get("country", "USA"))  # Returns default value
Output
John
30
None
USA

Checking Key Existence

Before accessing a value, you can check if a key exists in the dictionary using the 'in' keyword. This prevents KeyError exceptions when using square bracket access.

Example
# Checking if key exists
person = {"name": "John", "age": 30, "city": "New York"}

print("name" in person)
print("country" in person)
print("country" not in person)
Output
True
False
True

Accessing All Keys, Values, or Items

Dictionaries provide methods to access collections of their keys, values, or key-value pairs. The keys() method returns all keys, values() returns all values, and items() returns tuples of key-value pairs.

Example
# Accessing keys, values, and items
person = {"name": "John", "age": 30, "city": "New York"}

print("Keys:", list(person.keys()))
print("Values:", list(person.values()))
print("Items:", list(person.items()))
Output
Keys: ['name', 'age', 'city']
Values: ['John', 30, 'New York']
Items: [('name', 'John'), ('age', 30), ('city', 'New York')]
Test your knowledge: Access Dictionary Items
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 55 of 77
←PreviousPrevNextNext→