コンテンツにスキップ

Python辞書チートシート:使い方を簡単に説明

[

Python Dictionary Cheat Sheet

Creating a Dictionary

To create a dictionary in Python, you can use the curly brackets ”{ }” and separate key-value pairs with a colon ”:“. Here’s an example:

# Creating an empty dictionary
my_dict = {}
# Creating a dictionary with initial key-value pairs
my_dict = {'name': 'John', 'age': 25, 'country': 'USA'}

You can also use the dict() constructor to create a dictionary. For example:

# Creating an empty dictionary
my_dict = dict()
# Creating a dictionary with initial key-value pairs
my_dict = dict(name='John', age=25, country='USA')

Accessing Dictionary Values

To access values in a dictionary, you can use the square brackets ”[ ]” and provide the corresponding key. Here’s an example:

my_dict = {'name': 'John', 'age': 25, 'country': 'USA'}
# Accessing values
print(my_dict['name']) # Output: John
print(my_dict['age']) # Output: 25
print(my_dict['country']) # Output: USA

Modifying Dictionary Values

You can modify the values of a dictionary by assigning a new value to a specific key. Here’s an example:

my_dict = {'name': 'John', 'age': 25, 'country': 'USA'}
# Modifying values
my_dict['age'] = 30
my_dict['country'] = 'Canada'
print(my_dict) # Output: {'name': 'John', 'age': 30, 'country': 'Canada'}

Dictionary Methods

Python provides various methods to perform operations on dictionaries. Here are some commonly used methods:

MethodDescription
keys()Returns a list of all keys in the dictionary.
values()Returns a list of all values in the dictionary.
items()Returns a list of key-value pairs in the dictionary.
get(key, default)Returns the value for the given key. If the key does not exist, it returns the default value.
pop(key)Removes the key-value pair for the given key and returns the value.
update(other_dict)Updates the dictionary with the key-value pairs from the other dictionary.
clear()Removes all key-value pairs from the dictionary.
len()Returns the number of key-value pairs in the dictionary.

Here’s an example showing the usage of these methods:

my_dict = {'name': 'John', 'age': 25, 'country': 'USA'}
print(my_dict.keys()) # Output: ['name', 'age', 'country']
print(my_dict.values()) # Output: ['John', 25, 'USA']
print(my_dict.items()) # Output: [('name', 'John'), ('age', 25), ('country', 'USA')]
print(my_dict.get('name', 'Unknown')) # Output: John
print(my_dict.get('city', 'Unknown')) # Output: Unknown
removed_value = my_dict.pop('age')
print(removed_value) # Output: 25
other_dict = {'city': 'New York'}
my_dict.update(other_dict)
print(my_dict) # Output: {'name': 'John', 'country': 'USA', 'city': 'New York'}
my_dict.clear()
print(len(my_dict)) # Output: 0

Iterating Over Dictionary

You can iterate over a dictionary using a for loop. By default, the loop iterates over the keys of the dictionary. Here’s an example:

my_dict = {'name': 'John', 'age': 25, 'country': 'USA'}
# Iterating over keys
for key in my_dict:
print(key) # Output: name, age, country
# Iterating over values
for value in my_dict.values():
print(value) # Output: John, 25, USA
# Iterating over key-value pairs
for key, value in my_dict.items():
print(key, value) # Output: name John, age 25, country USA

Checking if a Key Exists

You can check if a specific key exists in a dictionary using the in keyword. Here’s an example:

my_dict = {'name': 'John', 'age': 25, 'country': 'USA'}
if 'age' in my_dict:
print('Age exists')
if 'city' not in my_dict:
print('City does not exist')

These are some basic operations you can perform with dictionaries in Python. By understanding the key concepts and utilizing the available methods, you can efficiently work with dictionaries to store and retrieve data in your Python programs.