What is the Difference Between List and Dictionary in Python?

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Dictionaries vs Lists in Python

PointsListDictionary
BasicsList refers to the collection of index value pair-like arrays in C/C++/Java.Dictionary refers to the hashed structure of various pairs like key-value pairs.
CreationList is initialized with [], and elements are separated by ','.Dictionary is created by placing elements in {}, data is added as key-value pair, and each pair is separated by ','.
AccessingList values can be accessed by numeric indexes.Dictionary items can be accessed by using key values. Key values can be of any data type.
Ordering of ElementsThe order of elements in the list is always maintained.We don’t have any guarantee of maintaining the order of the available elements.
PropertiesList is ordered, mutable, and allows duplicate values.In a dictionary, the dictionary is unordered and mutable, but the dictionary doesn't allow duplicate values with the same key in it.
Store the dataLists are used to store the data, which should be ordered and sequential.It stores large amounts of data for easy and quick access.

Examples of lists and dictionaries in Python

Example 1: Creating and Accessing values of list and dictionaries in Python

Code:

Output:

Explanation:

The following python code shows us how a list is implemented, how to access the list items with numeric indexing, and the multiple dimension of the list.

Input:

Output:

Explanation: In the following python code, we have declared two dictionaries. In dict, d1 takes numeric values as its keys, i.e., {1,2,3}, whereas in dict d2, it takes string values as its keys. Dictionary d1 can be considered like a list, but the fact is it is dict.

The above example shows the difference between a list and a dictionary.

Example 2: space-time trade-off difference between a list and a dictionary in python

Code:

Output:

Explanation:

In the following python code, we have seen that the dictionary took less time than a list. The list took more time to fetch a single element from the list than that of the dictionary because the dictionary uses a hashtable for implementing the arrangement.

Learn More

  • To understand the List concept, Click Here.
  • To know more about what is Dictionary in python? Click Here.
  • To know about Data Structures in Python, Click Here.

Conclusion

  • In this article, we have seen the difference between a list and a dictionary in python.
  • Later, we have given some examples that will show the differences between both data structures, i.e., list and dictionary.
  • List is a data structure in python that refers to the collection of index value pair-like arrays in C/C++/Java, whereas dictionaries are the data structures that refer to the hashed structure of various pairs like key-value pairs.
  • Lists are used to store the data, which should be ordered and sequential. On the other hand, dictionary is used to store large amounts of data for easy and quick access.
  • List is ordered and mutable, whereas dictionaries are unordered and mutable.