A dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and it maps to a specific value. Dictionaries are defined using curly braces {}
and can be created using the dict()
constructor. Here are key aspects of dictionaries:
Creating a Dictionary:
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
Creating a Dictionary with dict()
Constructor:
another_dict = dict(name='Jane', age=30, city='San Francisco')
Accessing Values:
print(my_dict['name']) # Output: 'John'
Adding or Modifying Values:
my_dict['occupation'] = 'Engineer'
my_dict['age'] = 26
Removing Key-Value Pairs:
del my_dict['city'] # Removes the 'city' key-value pair
Mapping Relationships:
Use dictionaries when you need to establish a mapping between keys and values, such as representing properties of an object.
Fast Lookup:
Dictionaries provide constant-time complexity for key lookup, making them ideal for scenarios where fast access to data is crucial.
Dynamic Data:
When dealing with data that may change frequently or has varying attributes, dictionaries provide flexibility.
keys()
Returns a view of all the keys in the dictionary.
values()
Returns a view of all the values in the dictionary.
items()
Returns a view of all key-value pairs as tuples.
get(key, default)
Returns the value for the specified key, or a default value if the key is not present.
PHP:
$my_dict = array('name' => 'John', 'age' => 25, 'city' => 'New York');
JavaScript:
var my_dict = {'name': 'John', 'age': 25, 'city': 'New York'};
What is a Python dictionary, and how is it defined?
Answer: A dictionary is an unordered collection of key-value pairs defined using curly braces {}
.
How do you access the value associated with a specific key in a dictionary?
Answer: Use square brackets and the key, like my_dict['name']
.
What happens if you try to access a key that is not present in the dictionary?
Answer: It raises a KeyError
. To avoid this, you can use the get()
method.
Can a dictionary have duplicate keys?
Answer: No, each key in a dictionary must be unique.
How do you add a new key-value pair to a dictionary?
Answer: Use the assignment operator, like my_dict['occupation'] = 'Engineer'
.
How do you remove a key-value pair from a dictionary?
Answer: Use the del
statement, like del my_dict['city']
.
Explain the purpose of the keys()
method in dictionaries.
Answer: It returns a view of all the keys in the dictionary.
What is the significance of the values()
method in dictionaries?
Answer: It returns a view of all the values in the dictionary.
Can you use a list or another dictionary as a value in a dictionary?
Answer: Yes, dictionaries are versatile and can contain values of any data type, including lists or other dictionaries.
How do you iterate over key-value pairs in a dictionary?
Answer: Use the items()
method to get a view of key-value pairs, then iterate over it using a loop.
These questions cover various aspects of dictionaries in Python, including their definition, usage, methods, and comparisons with similar data structures in PHP and JavaScript.