List vs Dictionary In Python Complete Comparision

In this article, I will discuss List vs Dictionary in Python. I will be discussing the complete difference between a list and a dictionary in Python with examples. You will learn when you should use a list and when to use the dictionary.

List vs Dictionary

What is List

Lists[1] are declared in other languages in the same way arrays are. To make lists more useful in Python, they don’t have to be uniform all the time. DataTypes such as Integers, Strings, and Objects can all be found in a single list. It is possible to make changes even after a list has been created because it is mutable.

A list is a sequence of items that can be changed and rearranged. As a result, it’s searchable, sliced, and rearranged as needed.

In the list, the element’s position on the list determines how to get at it. Due to the fact that Python lists already exist, you don’t have to worry about creating them manually like in other languages.

No matter what type of object you want to store in a list, you can do it with lists. Using them is as simple as using strings, and because they’re variable in length, they grow and shrink based on the amount of time they’re put to use.

When declaring a list of variables, use square brackets [] after the variable name. Let us see the list in the example code below.

# Creating a List in Python

List = ["Coduber", "Get", "To", "Code", 1, 2, 3]
print("Printing List Containing Different Types of Elements: ")
# Initializing the Two Dimensional List 
List = [['Coduber', 'With', 'Code'] , [1,2,3,4,5]]
print("\nTwo Dimensional List: ")


Printing List Containing Different Types of Elements: 
['Coduber', 'Get', 'To', 'Code', 1, 2, 3]     

Two Dimensional List:
[['Coduber', 'With', 'Code'], [1, 2, 3, 4, 5]]
['Coduber', 'With', 'Code']
[1, 2, 3, 4, 5]

What is Dictionary?

Python’s Dictionary, on the other hand, is a key:value pair of an unordered collection of data values used to store data values like a map.

The dictionary includes key-value pairs to help it run more efficiently. There is no comma between the keys in a Dictionary rather they are separated using a colon and keys, but there is one comma between each key in a Dictionary.

A dictionary is a key:value pair, like an associative array. A dictionary is like an address book where you can get someone’s address or contact information just by knowing their name (details).

The key must be unique since it fails the purpose to find the relevant information about the address if we take the above example. And you will not be able to find the address if keys(name) are the same in the dictionary.

Though as per dictionary structure you can only have a unique key and you will not be allowed to add the duplicate key in the dictionary. Let us see the dictionary in below example code.

#Demonstrating Dictionary in Python
#Initializing Dictionary in Python
Dict = {1: 'Coduber', 2: 'You', 3: 'Can', 4:'Code'}
print("Printing the Dictionary containing Keys and Values Pairs ")
# Initializing a Dictionary 
# with Alphanumeric keys
Dict = {'Website': 'Coduber', 1: [2,3,5,6]}
print("\nDictionary with the use of Alphanumeric Keys: ")

#Printing Dict Value using Key

#Printing Dict All Values
print("\nPrinting All values in Dict:")

#Printing Dict All Keys
print("\nPrinting All Keys in Dict:")


Printing the Dictionary containing Keys and Values Pairs 
{1: 'Coduber', 2: 'You', 3: 'Can', 4: 'Code'} 

Dictionary with the use of Alphanumeric Keys: 
{'Website': 'Coduber', 1: [2, 3, 5, 6]}       


Printing All values in Dict:
dict_values(['Coduber', [2, 3, 5, 6]])

Printing All Keys in Dict:
dict_keys(['Website', 1])
List vs Dictionary In Python Complete Comparision

Difference Between Dictionary and List

Now let us see the difference between list and dictionary in Python. I will be listing all the differences in the below table that will help you understand better.

A dictionary is a key-value pair hashed structure.In Python, a list is a collection of index value pairs, similar to an array in Other Languages.
Elements of any dictionary can only be accessed using its key and value pair only.Elements can be accessed using index values or indices numbers in List.
Each key-value pair is separated by commas “, ” and the dictionary is built by inserting components in as “key”:”value.” inside {} bracesPlace elements in [ ] separated by commas “, “ to make a list.
There is no guarantee that the sequence will be maintained.The sequence in which the elements are entered is preserved.
The keys of a dictionary can be of any data type, including numeric.The indices of a list are numbers that begin with the number 0.
Dictionary is more efficient to use if you have a large lookup to perform.The list is not as efficient as Dictionary.

That is all for all the differences between a list and a dictionary (list vs dictionary). You have learned when you should use a list and when to use a dictionary.

Please follow us on Facebook and Twitter. We will be soon coming with Free Courses on Python on all the topics such as Beginner, Intermediate, and Advance. Keep following us for further updates. Also, signup for the below newsletter to keep posted once we launch the courses.

Further Read:

  1. How to Split a List in Python – 3 Ways
  2. How to Clone or Copy a List in Python (Shallow/Deep)

Leave a Comment