Python lists and dictionaries are two of Python’s most used data structures. In this article, we will discuss list vs dictionary in Python to compare definition, syntax, mutability, and performance.
Python List vs Dictionary Summary
Following are some of the major differences between a Python list and a dictionary.
Python List | Python Dictionary |
Lists are defined using square brackets [] and the list() function. | A Python dictionary is defined using the curly braces {} or the dict() function. |
A Python list allows random access to elements using indices. | A dictionary allows random access to values using keys. |
Lists allow duplicate elements. | A dictionary doesn’t allow duplicate keys. It allows duplicate values. |
Lists provide faster performance while initialization, accessing elements, and iterating. | The performance of dictionary operations is a bit slow. |
Lists are mutable. | Dictionaries are also mutable. |
List vs Dictionary: Syntax and Definition
A Python list is a collection object used to store data in a sequential manner. In a list, each element has a particular position and we can access the data randomly using their position. On the other hand, a Python dictionary contains key-value pairs that don’t have a particular position.
We define a list using the square brackets whereas we use curly braces to create a dictionary. You can observe this in the following example.
myList=[1,2,3,4,5,6]
myDict={0:1,1:2,2:3,3:4,4:5,5:6}
We can also create a list using the list()
function. The list()
function takes a container object like a set, tuple, etc as its input and returns a list. To create a dictionary, we can use the dict()
function. The dict()
function takes a list of tuples as its input argument and returns a dictionary. You can observe this in the following example.
myList=list((1,2,3,4,5,6))
myDict={dict([(0,1),(1,2),(2,3),(3,4),(4,5),(5,6)])
We use the index of an element to access an element in a list. In a dictionary, we use the key to access the value as shown below.
myList[0] #0 is position
myDict[0] # 0 is key
Python List vs Dictionary: Mutability
Lists and dictionaries both are mutable.
We can add elements to a list using the append()
and extend()
methods. The append()
method, when invoked on a list, takes an element as its input and adds it to the list. The extend()
method, when invoked on a list, takes a container object and adds all the elements of the container object to the list. You can observe this in the following example.
myList=[1,2,3,4,5,6]
myList.append(10) #output becomes [1,2,3,4,5,6, 10]
myList.extend([10,11,12,13])# output is [1,2,3,4,5,6,10,11,12,13]
To add a key-value pair into a Python dictionary, we can directly assign the key-value pair to the dictionary object using the assignment operator. We can also add elements to a dictionary using the update()
method. The update()
method, when invoked on a dictionary, takes another dictionary object as its input. After execution, it adds a key-value pair from the new dictionary to the original dictionary object if the key isn’t already present in the original dictionary object. Otherwise, it updates the values according to the keys in the new dictionary object. You can observe this in the following example.
myDict={0:1,1:2,2:3,3:4,4:5,5:6}
myDict.update({9:81,10:100})#output is {0:1,1:2,2:3,3:4,4:5,5:6,9:81,10:100}
We can also remove elements from a dictionary as well as a list using different methods defined for them. Thus, lists and dictionaries both are mutable.
List vs Dictionary: Performance
If we consider performance, lists are more efficient than dictionaries most number of times.
To create a list having 6 elements, the Python interpreter takes 50 nanoseconds. On the contrary, it takes 247 nanoseconds for creating a dictionary with six key-value pairs. You can observe this in the following example.
Accessing an element from a list and a dictionary have almost identical performance. However, the list also wins this battle by a slight margin. You can observe this in the following example.
Lists perform better than dictionaries while iteration. Just iterating through the elements of a list takes around 108 ns. On the other hand, iterating through the keys of a dictionary with 6 key-value pairs takes around 132 ns. You can observe this in the following example.
When to Use List vs Dictionary in Python?
If the values that you want to store have an inherent order and you want random access, you can use a list. When you have key-value pairs that don’t have a particular order, you can use a Python dictionary.
If you need to remove elements from the program frequently, you should use a dictionary. Python lists have more time complexity than dictionaries while removing an element.
Conclusion
In this article, we discussed different aspects of Python list vs dictionary to discuss the syntax, definition, Mutability, and performance of both these data structures. To learn more about Python programming, you can read this article on tuple vs list in Python. You might also like this article on for loop vs while loop in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.