Junior — Middle
What data structure do you prefer to use in your projects and why?
sobes.tech AI
Answer from AI
In Python, I often prefer to use lists (list) and dictionaries (dict) because they are versatile and efficient for most tasks. Lists are convenient for ordered collections with quick access by index, while dictionaries are used for storing key-value pairs with fast lookup.
If an immutable structure is needed, I use tuples (tuple). For more complex cases, sets (set) for unique elements or specialized structures from the collections module (such as deque for queues, defaultdict for dictionaries with defaults) are used.
An example of using a dictionary to count the number of elements:
items = ['apple', 'banana', 'apple', 'orange']
count = {}
for item in items:
count[item] = count.get(item, 0) + 1
print(count) # {'apple': 2, 'banana': 1, 'orange': 1}