Difference Between List and Dictionary in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What is List in Python?

In Python, a list is an ordered, mutable collection of elements, enclosed in square brackets [], allowing duplicates and supporting various operations like appending, removing, and slicing.

Let’s take two simple examples to understand operations on Lists in Python.

  • Creation and Basic Operations:
  • Slicing and Indexing:

To learn more, you can visit: List in Python.

What is Dictionary in Python?

A Python dictionary is an unordered collection that stores data as key pairs, differing from other data types that store single values. In a dictionary, each key pair is delimited by a colon (:), and individual keys are distinguished by commas. This structure offers enhanced optimization.

Example:

Key Difference Between List and Dictionary in Python

The difference between list and dictionary in Python lies in their structure; while a list is an ordered collection indexed by position and allowing duplicates, a dictionary is an unordered collection of unique key pairs.

Lists are best for sequential data, while dictionaries are tailored for data retrieval by specific identifiers.

Dictionaries vs Lists in Python

Difference Between List and Dictionary 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.
IndexingThe indices of lists in python start from zero and are of integer type.The keys in dictionaries are used to access key-value pairs and they can be of any type.
Sortingsort() method sorts the elements of the list.sort() method sort the dictionary elements on the basis of their keys.
ReversalThe reverse() method reverses the list elements.Since dictionary elements are unordered, reversing the elements does not make any sense.

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.

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.