콘텐츠로 건너뛰기

파이썬 딕셔너리 치트시트: 쉽게 이해하는 사용법

[

Python Dictionary Cheat Sheet

Introduction

In Python, a dictionary is an unordered collection of key-value pairs. It allows us to store and retrieve data by using the keys as references. This cheat sheet will provide an overview of dictionary operations and techniques to manipulate dictionary data in Python.

Creating a Dictionary

To create a dictionary in Python, we can use the following syntax:

my_dict = {'key1': 'value1', 'key2': 'value2'}
  • The keys and values are separated by a colon (:).
  • Each key-value pair is separated by a comma.

We can also create an empty dictionary using the constructor syntax:

my_dict = dict()

Accessing Dictionary Values

To access the values stored in a dictionary, we can use the keys as references. Here is a sample code:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict['name']) # Output: John
print(my_dict['age']) # Output: 30

Adding and Modifying Dictionary Elements

We can add new key-value pairs to a dictionary or modify the values of existing keys. Here’s how:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['occupation'] = 'Software Engineer' # Adding a new key-value pair
my_dict['age'] = 31 # Modifying the value of an existing key
print(my_dict)

Output:

{'name': 'John', 'age': 31, 'city': 'New York', 'occupation': 'Software Engineer'}

Dictionary Methods

Python provides several built-in methods to perform various operations on dictionaries. Some commonly used methods include:

  • keys(): Returns a list of all the keys in the dictionary.
  • values(): Returns a list of all the values in the dictionary.
  • items(): Returns a list of all key-value pairs in the dictionary.

Here is an example usage of these methods:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.keys()) # Output: ['name', 'age', 'city']
print(my_dict.values()) # Output: ['John', 30, 'New York']
print(my_dict.items()) # Output: [('name', 'John'), ('age', 30), ('city', 'New York')]

Dictionary Comprehension

Similar to list comprehension, we can also use dictionary comprehension to create dictionaries in a concise manner. Here’s an example:

my_dict = {x: x**2 for x in range(5)}
print(my_dict)

Output:

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Removing Dictionary Elements

To remove elements from a dictionary, we can use the del keyword followed by the key we want to delete. Here’s an example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
del my_dict['age'] # Removing a key-value pair
print(my_dict)

Output:

{'name': 'John', 'city': 'New York'}

Checking If a Key Exists

We can use the in keyword to check if a key exists in a dictionary. Here’s an example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
if 'age' in my_dict:
print('Age is present in the dictionary.')
else:
print('Age is not present in the dictionary.')

Output:

Age is present in the dictionary.

Conclusion

In this tutorial, we covered the basics of working with dictionaries in Python. We learned how to create dictionaries, access their values, add and modify elements, use built-in methods, perform dictionary comprehension, remove elements, and check if a key exists. By understanding these concepts and techniques, you will be better equipped to manipulate dictionary data effectively in Python.

Remember to keep practicing and exploring more advanced topics to enhance your Python programming skills. Happy coding!