Difference between List and Dictionary in Python - GeeksforGeeks

21 Sep, 2021
Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it a most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.Example: 
Python3
# Python program to demonstrate# Lists  # Creating a List with# the use of multiple valuesList = ["Geeks", "For", "Geeks"]print("List containing multiple values: ")print(List[0]) print(List[2])   # Creating a Multi-Dimensional List# (By Nesting a list inside a List)List = [['Geeks', 'For'] , ['Geeks']]print("\nMulti-Dimensional List: ")print(List)
Output: 
List containing multiple values: 
Geeks
Geeks

Multi-Dimensional List: 
[['Geeks', 'For'], ['Geeks']]
Dictionary in Python on the other hand is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’.Example: 
Python3
# Python program to demonstrate# dictionary  # Creating a Dictionary # with Integer KeysDict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}print("Dictionary with the use of Integer Keys: ")print(Dict)   # Creating a Dictionary # with Mixed keysDict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}print("\nDictionary with the use of Mixed Keys: ")print(Dict)
Output: 
Dictionary with the use of Integer Keys: 
{1: 'Geeks', 2: 'For', 3: 'Geeks'}

Dictionary with the use of Mixed Keys: 
{1: [1, 2, 3, 4], 'Name': 'Geeks'}
 
 
 
It is more efficient to use a dictionary for lookup of elements because it takes less time to traverse in the dictionary than a list.For example, let’s consider a data set with 5000000 elements in a machine learning model that relies on the speed of retrieval of data. To implement this we have to choose wisely between two data structure i.e. list and dictionary. The dictionary is preferred because of less time and less space storage as dictionaries are implemented in the form of hash tables from python3.6 so it is never a space-time trade-off problem in dictionaries.Example 1: 
Python3
# Program to demonstrate# space-time trade-off between# dictionary and list  # To calculate the time# differenceimport time  # Creating a dictionaryd ={'john':1, 'alex':2} x = time.time() # Accessing elementsprint("Accessing dictionary elements:")for key in d:    print(d[key], end=" ") y = time.time()print("\nTime taken by dictionary:", y-x) # Creating a Listc =[1, 2] x = time.time() print("\nAccessing List elements:")for i in c:    print(i, end=" ")     y = time.time()print("\nTime taken by dictionary:", y-x)
Output: 
Accessing dictionary elements:
1 2 
Time taken by dictionary: 1.0013580322265625e-05

Accessing List elements:
1 2 
Time taken by dictionary: 3.5762786865234375e-06
Example 2: 
Python3
# Program to fetch particular# elements of structure  import time  # Creating dictionary and listdict_name ={"bob":12, "john":11}list_name =[2, 3, 4, 5, 1] # Time taken by dictionaryx = time.time()L = dict_name["bob"]y = time.time()print("Time taken by dictionary:", y-x) # Time taken by listx = time.time()L = list_name[2]y = time.time()print("\nTime taken by list:", y-x)
Output: 
Time taken by dictionary: 9.5367431640625e-07

Time taken by list: 4.76837158203125e-07
Note: It took more time for fetching a single element in a list than that of a dictionary because dictionary uses hashtable for implementing the arrangement. 
sooda367
adnanirshad158
python-dict
python-list
Python
python-dict
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python
sum() function in Python
How To Convert Python Dictionary To JSON?