Python - Dictionaries
Why should I learn to solve Python: Dictionaries technical interview questions?
Learn and practise solving Python: Dictionaries technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical Python: Dictionaries technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved Python: Dictionaries technical interview questions and answers with a short answer description. You can download Python: Dictionaries technical interview questions and answers as PDF files or e-books.
How do I answer Python: Dictionaries technical interview questions from various companies?
You can answer all kinds of Python: Dictionaries technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Dictionaries technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
In Python, a dictionary is a collection of key-value pairs. Each key must be unique within a dictionary, and it maps to a specific value. Dictionaries are defined using curly braces {}.
# Example dictionary
person = {
"Name": "John",
"Age": 25,
"City": "New York"
}
# Accessing values using keys
print("Name:", person["Name"])
print("Age:", person["Age"])
print("City:", person["City"])
Output:
Name: John Age: 25 City: New York
In Python, you can create an empty dictionary using curly braces {} or by using the built-in dict() constructor.
# Using curly braces
empty_dict1 = {}
print("Empty Dictionary 1:", empty_dict1)
# Using dict() constructor
empty_dict2 = dict()
print("Empty Dictionary 2:", empty_dict2)
Output:
Empty Dictionary 1: {} Empty Dictionary 2: {}
In Python, dictionaries are a collection of key-value pairs. Here are key characteristics of dictionaries:
- Unordered: Dictionaries are unordered, meaning that the order of elements is not guaranteed.
- Mutable: You can modify dictionaries by adding, updating, or removing key-value pairs.
- Dynamic: Dictionaries can grow or shrink in size as needed.
- Keys: Keys in a dictionary must be unique and immutable (strings, numbers, or tuples).
- Values: Values in a dictionary can be of any data type and can be duplicated.
Here's an example demonstrating the key characteristics:
# Creating a dictionary
student_info = {
"Name": "John",
"Age": 25,
"City": "New York"
}
# Accessing values using keys
print("Name:", student_info["Name"])
print("Age:", student_info["Age"])
print("City:", student_info["City"])
# Modifying values
student_info["Age"] = 26
student_info["Grade"] = "A"
# Printing the modified dictionary
print("Modified Dictionary:", student_info)
# Removing a key-value pair
del student_info["City"]
# Printing the final dictionary
print("Final Dictionary:", student_info)
Output:
Name: John Age: 25 City: New York Modified Dictionary: {'Name': 'John', 'Age': 26, 'City': 'New York', 'Grade': 'A'} Final Dictionary: {'Name': 'John', 'Age': 26, 'Grade': 'A'}
In Python, dictionaries, lists, and tuples are three distinct data types, each with its own characteristics. Here's a comparison between a dictionary and a list or tuple:
1. Structure:
# Dictionary
student_info = {"Name": "John", "Age": 25, "City": "New York"}
# List
grades = [90, 85, 92, 88]
# Tuple
coordinates = (3, 5)
2. Purpose:
A dictionary is suitable for storing related key-value pairs. A list is used to store an ordered collection of items, and a tuple is an immutable ordered collection.
3. Mutability:
# Dictionaries are mutable
student_info["Age"] = 26
# Lists are mutable
grades[1] = 87
# Tuples are immutable; this will raise an error
coordinates[0] = 4
4. Syntax:
# Accessing dictionary values using keys
name = student_info["Name"]
# Accessing list elements using indices
grade = grades[1]
# Accessing tuple elements using indices
x = coordinates[0]
5. Examples:
# Adding a new key-value pair to a dictionary
student_info["Grade"] = "A"
# Appending an element to a list
grades.append(95)
# Concatenating tuples
new_coordinates = coordinates + (7, 2)
6. Output:
Dictionary: {'Name': 'John', 'Age': 26, 'City': 'New York', 'Grade': 'A'} List: [90, 87, 92, 88, 95] Tuple: (3, 5, 7, 2)