Difference Between List and Dictionary in Python

Difference Between List and Dictionary in Python

  1. Difference Between List and Dictionary Based on Creation in Python
  2. Difference Between List and Dictionary Based on Accessing the Elements in Python
  3. Difference Between List and Dictionary Based on Operations in Python
  4. Difference Between List and Dictionary Based on Time in Python

Lists and dictionaries are two data structures in Python. Both are mutable, so they can be updated as per the code’s need; both are used to store a collection or a group of elements.

This tutorial will elaborate on the differences between a dictionary and a list in Python based on different parameters.

Difference Between List and Dictionary Based on Creation in Python

Lists are created by placing all the elements between square brackets [ ]. A comma separates the elements in the list.

Dictionaries are created by placing all the elements between curly brackets { }. Elements are stored in the form of key-value pairs, each separated by a comma.

Example:

lst = [2,4,6,8]
dict = {'a':2, 'b':4, 'c':6, 'd':8}
print(lst, '\n', dict)

Output:

[2, 4, 6, 8]
 {'a': 2, 'b': 4, 'c': 6, 'd': 8}

Difference Between List and Dictionary Based on Accessing the Elements in Python

Every element in any given list has a specific index value. To access a particular element in a list, we must use the element’s index value.

Also, the index values in the list start with 0, not 1. That means the first element in the list has an index value of 0.

In the dictionary, all the elements are stored in the form of key:value pairs. To access a value or an element, we use its associated key.

Example:

lst = [2,4,6,8]
dict = {'a':2, 'b':4, 'c':6, 'd':8}
print(lst[1], dict['c'])

Output

4 6

Note that for mentioning the index value or the key, square brackets [ ] are used.

Difference Between List and Dictionary Based on Operations in Python

We can use many operations on lists, including min(), max(), len(), etc. Lists can also be traversed using the for loop.

Also, operations used only on mutable objects like del, append(), insert() can be applied on lists.

Dictionaries don’t have any operations that can be performed on them since it is considered a mapping data type used to store values with their keys.

Example of some operations performed on a list:

lst = [2,4,6,8]
print(max(lst),len(lst))

Output:

8 4

Difference Between List and Dictionary Based on Time in Python

Lists and Dictionaries can also be differentiated based on time and space. Time and space are required while accessing the elements in both lists and dictionaries.

Dictionaries are more coherent and favorable in this case.

In the case of lists, the whole list is traversed to access a specific element; therefore, it takes more time and space. We can observe this when the size of the given data or the dataset is huge.

Whereas, in a dictionary, the data is stored in hash tables with definite key values associated with each element. Due to this, elements can be accessed easily without any space-time tradeoff.

If one wishes to store a large amount of data, then a dictionary is more suitable as there will be quick access due to the mapping of elements in the hash table. And if one wants to store the data in a definite order and perform various operations on the data, then a list is more suitable.

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python
  • Related Article - Python Dictionary

  • Check if a Key Exists in a Dictionary in Python
  • Convert a Dictionary to a List in Python
  • Get All the Files of a Directory
  • Find Maximum Value in Python Dictionary
  • Delete a File and Directory in Python
  • Sort a Python Dictionary by Value