Sobes.tech
Junior

What is the difference between a dictionary and an array in Python?

sobes.tech AI

Answer from AI

In Python, dictionary (dict) and list are different data structures with different purposes:

  • List — an ordered collection of elements, accessed by index (an integer). Elements can repeat, and order matters.

  • Dictionary (dict) — an unordered (ordered since Python 3.7) collection of key-value pairs, where keys are unique and used for quick access to values.

Example:

# List
fruits = ['apple', 'banana', 'cherry']
print(fruits[1])  # banana

# Dictionary
person = {'name': 'Ivan', 'age': 30}
print(person['name'])  # Ivan

Thus, if you need to store an ordered set of elements with position-based access — use a list. If you need to store data with unique keys for quick lookup — use a dictionary.