Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Built-in Dictionary Functions & Methods in Python
Python dictionaries come with built-in functions and methods that make working with key-value pairs efficient and straightforward. These functions help you manipulate, query, and transform dictionary data.
Built-in Dictionary Functions
Python provides several built-in functions that work with dictionaries ?
len() Function
student = {'name': 'Alice', 'age': 20, 'grade': 'A'}
print(len(student))
3
str() Function
student = {'name': 'Alice', 'age': 20}
print(str(student))
print(type(str(student)))
{'name': 'Alice', 'age': 20}
<class 'str'>
type() Function
student = {'name': 'Alice', 'age': 20}
print(type(student))
<class 'dict'>
Dictionary Methods
Dictionary methods are called directly on dictionary objects and provide powerful ways to manipulate data ?
Basic Operations
# clear() - removes all elements
student = {'name': 'Alice', 'age': 20}
student.clear()
print(student)
# copy() - creates a shallow copy
original = {'a': 1, 'b': 2}
copied = original.copy()
print(copied)
{}
{'a': 1, 'b': 2}
Accessing Values Safely
student = {'name': 'Alice', 'age': 20}
# get() - returns value or default
print(student.get('name'))
print(student.get('grade', 'Not Available'))
# setdefault() - gets or sets default value
student.setdefault('grade', 'A')
print(student)
Alice
Not Available
{'name': 'Alice', 'age': 20, 'grade': 'A'}
Dictionary Views
student = {'name': 'Alice', 'age': 20, 'grade': 'A'}
# keys() - returns dictionary keys
print(list(student.keys()))
# values() - returns dictionary values
print(list(student.values()))
# items() - returns key-value pairs
print(list(student.items()))
['name', 'age', 'grade']
['Alice', 20, 'A']
[('name', 'Alice'), ('age', 20), ('grade', 'A')]
Updating Dictionaries
student = {'name': 'Alice', 'age': 20}
additional_info = {'grade': 'A', 'major': 'Computer Science'}
# update() - adds key-value pairs from another dictionary
student.update(additional_info)
print(student)
# fromkeys() - creates dictionary from keys
subjects = dict.fromkeys(['Math', 'Science', 'English'], 'Pending')
print(subjects)
{'name': 'Alice', 'age': 20, 'grade': 'A', 'major': 'Computer Science'}
{'Math': 'Pending', 'Science': 'Pending', 'English': 'Pending'}
Summary of Dictionary Functions and Methods
| Function/Method | Purpose | Returns |
|---|---|---|
len(dict) |
Count items | Integer |
str(dict) |
String representation | String |
dict.get(key) |
Safe value access | Value or None |
dict.keys() |
All keys | Dict keys view |
dict.values() |
All values | Dict values view |
dict.items() |
Key-value pairs | Dict items view |
Conclusion
Python's built-in dictionary functions and methods provide comprehensive tools for dictionary manipulation. Use get() for safe access, update() for merging dictionaries, and view methods like keys() and items() for iteration.
Advertisements
