List and Dictionary are the most commonly used data structures in python. In this post, we learn the main key difference between both of them in detail with examples.

So let’s start learning from the very basics…

Definition of List and Dictionary

Definition of List: A list is one type of built-in data structure in python and it is used to store an ordered collection of items in a single variable.

Definition of Dictionary: A dictionary is a built-in data structure in python and it is used to store an unordered collection of items in the form of a key: value pair.

Create a List and Dictionary

Create a List: Write comma-separated values inside a square bracket ([ ]) to create a list in python.

l1 = [1,4,5,"allinpython",9,2.1]
print(l1)

Output:

[1, 4, 5, 'allinpython', 9, 2.1]

Create a Dictionary: To create a dictionary in python write comma-separated data in form of a key: value pair inside the curly bracket ({ }).

user1 = {'name':'joy','age':19,'collage':'california'}
print(user1)

Output:

{'name': 'joy', 'age': 19, 'collage': 'california'}

Ordering of List and Dictionary:

Lists are ordered collections of items, where each item has an assigned index value starting from 0.

Dictionaries are unordered collections of items in key-value pairs, where the order of items is not important (there is no indexing).

Access Elements of the List and Dictionary

In a list, items can be accessed and manipulated using their index value.

l1 = [1, 4, 5, 'allinpython', 9, 2.1]
print(l1[3])  #access elements of the list (index value of 'allinpython' is 3)

l1[0] = 'one' #change value using there index (1 is replace with 'one')
print(l1)

Output:

allinpython
['one', 4, 5, 'allinpython', 9, 2.1]

In a dictionary, items can only be accessed using their keys.

user1 = {'name':'joy','age':19,'collage':'california'}
print(user1['name']) #acess name
user1['name'] = 'roy' #change name of user1
print(user1)

Output:

joy
{'name': 'roy', 'age': 19, 'collage': 'california'}

Performance of List and Dictionary

Dictionaries are faster when searching for items based on their keys.

Lists are faster when accessing items based on their index.

Summary

ListDictionary
List is one type of built-in data structure in python that is used to store an ordered collection of items in a single variable.A dictionary is a built-in data structure in python that is used to store an unordered collection of items in the form of a key: value pair.
Write comma-separated values inside a square bracket ([ ]) to create a list in python.To create a dictionary in python write comma-separated data in form of a key: value pair inside the curly bracket ({ }).
List is ordered (There is indexing)Dictionary is unordered (There is no indexing)
In a list, items can be accessed and manipulated using their index value.In a dictionary, items can only be accessed using their keys.
Dictionaries are faster when searching for items based on their keys.Lists are faster when accessing items based on their index.
Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment