Data Structures Practice Problems
Python Practice Set: Data Structures (Related Questions)
Test your ability to choose and use the right data structure (Lists, Tuples, Sets, and Dictionaries) for different scenarios.
1. Comparison & Selection
Q1: Which data structure would you use to store a collection of unique user IDs where order doesn't matter?
Answer: A Set, because sets automatically handle uniqueness and are optimized for membership testing.
Q2: Which data structure is best for storing a fixed geometric coordinate (x, y) that should not be modified?
Answer: A Tuple, because it is immutable and clearly signals that the data should remain constant.
Q3: Match the data structure to its syntax and properties:
| Structure | Syntax | Properties |
| :--- | :--- | :--- |
| List | [] | Ordered, Changeable, Allows Duplicates |
| Tuple | () | Ordered, Unchangeable, Allows Duplicates |
| Set | {} | Unordered, Unindexed, No Duplicates |
| Dictionary | {k:v} | Ordered (3.7+), Changeable, Key-Value Pairs |
2. Conversions & Operations
Q4: How do you quickly remove all duplicates from a list?
Answer: Convert the list to a set and then back to a list.
unique_list = list(set(original_list))
Q5: Predict the output:
x = [1, 2, 3]
y = x
y.append(4)
print(x)
Output:
[1, 2, 3, 4](Note: Lists are objects;y = xcreates a reference, not a copy. Changingychangesx)
Q6: What is the fastest way to check if an item exists in a large collection?
Answer: Using a Set or Dictionary keys. Membership testing (
item in collection) is $O(1)$ for sets/dicts but $O(n)$ for lists.
3. Advanced Challenges
Q7: Explain the difference between list.sort() and sorted(list).
Answer:
list.sort()sorts the list in-place and returnsNone.sorted(list)returns a new sorted list, leaving the original unchanged.
Q8: Give an example of a dictionary where the values are lists.
grades = {
"Alice": [85, 90, 88],
"Bob": [70, 75, 80]
}
4. Coding Challenges
Task 1: Frequency Counter
Write a function that counts the frequency of each word in a string using a dictionary.
def count_frequency(text):
words = text.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
print(count_frequency("apple banana apple cherry banana apple"))
# Output: {'apple': 3, 'banana': 2, 'cherry': 1}
Task 2: List of Tuples to Dictionary
Convert a list of tuples [("id", 1), ("name", "Tanis"), ("role", "Admin")] into a dictionary.
data = [("id", 1), ("name", "Tanis"), ("role", "Admin")]
result = dict(data)
print(result) # {'id': 1, 'name': 'Tanis', 'role': 'Admin'}
Task 3: Find Common Elements
Find common elements between two lists using set operations.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common = list(set(list1) & set(list2))
print(common) # [4, 5]
Task 4: Nested Access
Given the following structure, print the "city" of the second employee.
company = {
"employees": [
{"name": "Alice", "location": {"city": "New York"}},
{"name": "Bob", "location": {"city": "San Francisco"}}
]
}
print(company["employees"][1]["location"]["city"])