Junior — Middle
What is the difference between a list and a dictionary data structure in the context of programming?
sobes.tech AI
Answer from AI
A list and a dictionary are different data structures with different principles of organization and access to elements.
- List — an ordered collection of elements, accessed by index (an integer). Elements can repeat, and order matters.
- Dictionary — an unordered (in modern Python versions — ordered by insertion) collection of key-value pairs. Access to values occurs via the key, which can be a string, number, or other immutable type.
Example:
my_list = [10, 20, 30]
print(my_list[1]) # Outputs 20
my_dict = {'a': 10, 'b': 20}
print(my_dict['b']) # Outputs 20
Thus, a list is convenient for storing ordered data, and a dictionary is suitable for quick key-based lookup.