Sobes.tech
Intern

What is the difference between a list and a dictionary in Python?

sobes.tech AI

Answer from AI

In Python, a list and a dictionary are mutable data structures, but they differ in how they store and access elements.

  • List — an ordered collection of elements, accessed by index (an integer). Elements can repeat.
  • Dictionary — an unordered (since Python 3.7, maintains insertion order) collection of key-value pairs, where keys are unique and used to access values.

Example:

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

# dictionary
d = {'apple': 'red', 'banana': 'yellow'}
print(d['banana'])  # yellow

Thus, a list is convenient for storing ordered data, and a dictionary is useful for quick key-based lookup.