Getting Keys of Dictionary in Python
Introduction
In Python, dictionaries are a fundamental data structure that stores key-value pairs. They are used to store data in a flexible and efficient manner. One of the key features of dictionaries is that they allow you to access values by their corresponding keys. In this article, we will explore how to get keys of dictionary in Python.
Basic Dictionary Operations
Before we dive into getting keys of dictionary, let’s cover some basic dictionary operations:
- Creating a dictionary: You can create a dictionary using the
dict()function or by assigning key-value pairs to a variable. - Accessing values: You can access values by their corresponding keys using the square bracket notation
dictionary[key]. - Updating values: You can update values by assigning a new value to a key using the assignment operator
dictionary[key] = new_value. - Deleting values: You can delete values by using the
delstatement.
Getting Keys of Dictionary
Now that we have covered basic dictionary operations, let’s focus on getting keys of dictionary.
Method 1: Using Dictionary Keys
You can get keys of dictionary using the keys() method.
# Create a dictionary
person = {'name': 'John', 'age': 30, 'city': 'New York'}
# Get keys of dictionary
keys = person.keys()
# Print keys
print(keys) # Output: dict_keys(['name', 'age', 'city'])
Method 2: Using Dictionary Items
You can get keys of dictionary using dictionary items.
# Create a dictionary
person = {'name': 'John', 'age': 30, 'city': 'New York'}
# Get keys of dictionary
keys = list(person.items())
# Print keys
print(keys) # Output: [('name', 'John'), ('age', 30), ('city', 'New York')]
Method 3: Using Dictionary Values
You can get keys of dictionary using dictionary values.
# Create a dictionary
person = {'name': 'John', 'age': 30, 'city': 'New York'}
# Get keys of dictionary
keys = list(person.values())
# Print keys
print(keys) # Output: ['John', 30, 'New York']
Method 4: Using Dictionary Items as Keys
You can get keys of dictionary using dictionary items as keys.
# Create a dictionary
person = {'name': 'John', 'age': 30, 'city': 'New York'}
# Get keys of dictionary
keys = list(person.keys())
# Print keys
print(keys) # Output: ['name', 'age', 'city']
Important Points
- Dictionary keys are immutable: Dictionary keys are immutable, meaning they cannot be changed after creation.
- Dictionary values are mutable: Dictionary values are mutable, meaning they can be changed after creation.
- Dictionary keys are unique: Dictionary keys are unique, meaning each key corresponds to a unique value.
Conclusion
In this article, we have explored how to get keys of dictionary in Python. We covered basic dictionary operations, including creating dictionaries, accessing values, updating values, and deleting values. We also discussed how to get keys of dictionary using dictionary keys, dictionary items, dictionary values, and dictionary items as keys. By understanding how to get keys of dictionary, you can efficiently store and retrieve data in your Python programs.
Table: Dictionary Operations
| Operation | Description |
|---|---|
| Creating a dictionary | Assign key-value pairs to a variable or use the dict() function |
| Accessing values | Use the square bracket notation dictionary[key] |
| Updating values | Assign a new value to a key using the assignment operator dictionary[key] = new_value |
| Deleting values | Use the del statement |
Code Snippets
# Create a dictionary
person = {'name': 'John', 'age': 30, 'city': 'New York'}
# Get keys of dictionary
keys = person.keys()
# Print keys
print(keys) # Output: dict_keys(['name', 'age', 'city'])
# Get keys of dictionary using dictionary items
keys = list(person.items())
# Print keys
print(keys) # Output: [('name', 'John'), ('age', 30), ('city', 'New York')]
# Get keys of dictionary using dictionary values
keys = list(person.values())
# Print keys
print(keys) # Output: ['John', 30, 'New York']
# Get keys of dictionary using dictionary items as keys
keys = list(person.keys())
# Print keys
print(keys) # Output: ['name', 'age', 'city']
