Skip to content

Difference between list and dictionary in Python

  • by

The difference between lists and dictionaries in Python lies in their structure, access methods, mutability, and usage. Lists are ordered collections that store elements based on their positions, while dictionaries are unordered collections of key-value pairs accessed via unique keys.

Lists allow duplicate values and support indexing, while dictionaries require unique keys and support key-based access.

Both lists and dictionaries are mutable, but lists are commonly used for ordered data and tasks involving sequence manipulation, while dictionaries excel in data lookup and organizing structured information. Understanding these distinctions helps in choosing the appropriate data structure for Python programming.

Here’s a tabular format highlighting the differences between lists and dictionaries in Python:

#ListDictionary
StructureOrdered collectionUnordered collection of key-value pairs
SyntaxEnclosed in square brackets ([])Enclosed in curly braces ({})
ElementsCan be of different data typesKeys must be unique and values can be of any data type
AccessingElements accessed by indexElements accessed by key
MutabilityMutable (can modify elements)Mutable (can modify values)
OrderingPreserves order of elementsNo specific order of elements
Duplicate valuesAllows duplicate valuesKeys must be unique
Use caseSuitable for collections of similar items, where order mattersSuitable for data lookup based on keys

Here’s a comparison of the syntax differences between lists and dictionaries in Python:

  1. List Syntax:
    • List creation: Enclosed in square brackets []. Example: my_list = [1, 2, 3, 4]
    • Accessing elements: Done by index using square brackets []. Example: print(my_list[0])
    • Modifying elements: Assigning new values to specific indexes. Example: my_list[0] = 10
  2. Dictionary Syntax:
    • Dictionary creation: Enclosed in curly braces {} or using the dict() constructor. Example: my_dict = {'name': 'John', 'age': 25}
    • Accessing elements: Done by specifying the key inside square brackets []. Example: print(my_dict['name'])
    • Modifying elements: Assigning new values to specific keys. Example: my_dict['age'] = 30

Note: In addition to the above syntax, dictionaries also support using the dict() constructor or the dict keyword to create an empty dictionary or convert other iterable objects into a dictionary.

Difference between list and dictionary in Python example

Here’s an example that demonstrates the differences between lists and dictionaries in Python:

# Example using a List
my_list = [1, 2, 3, 4, 2]
print(my_list)
print(my_list[2])

my_list[0] = 10
print(my_list)              

# Example using a Dictionary
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
print(my_dict)
print(my_dict['age'])

my_dict['age'] = 30
print(my_dict)

Output:

Difference between list and dictionary in Python

Comment if you have any doubts or suggestions on this Python difference topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *