Difference Between List & Dictionary in Python
Hi! This short tutorial will show you the basic differences between a list and a dictionary in the Python programming language, by examining their respective attributes.
First, though, here is a quick overview:
Let’s get right into the discussion and Python code!
Python List
A Python list is used to store different data types, like strings, integers, in a variable. Python lists are mutable, which means they can be altered or modified. Below are examples of Python lists containing integers, strings and user-defined objects:
# list of integers int_list = [1,2,3,4,5,6,7] print(int_list) # [1, 2, 3, 4, 5, 6, 7] # list of strings str_list = ["apple", "banana", "mango", "peach", "orange"] print(str_list) # ['apple', 'banana', 'mango', 'peach', 'orange'] #user-defined class class Bio: def __init__(self, name, age): self.name = name self.age = age # list of objects my_list = [] my_list.append(Bio("Linda",32)) my_list.append(Bio("Mandi",23)) my_list.append(Bio("John",24)) my_list.append(Bio("Lisa",36)) my_list.append(Bio("Jason",40)) # for loop to print object list for l in my_list: print(l.name, l.age, sep = ",") # Linda,32 # Mandi,23 # John,24 # Lisa,36 # Jason,40
We can also create a Python list using the list() function:
# list of integers int_list = list((1,2,3,4,5,6,7)) print(int_list) # [1, 2, 3, 4, 5, 6, 7] # list of strings str_list = list(("apple", "banana", "mango", "peach", "orange")) print(str_list) # ['apple', 'banana', 'mango', 'peach', 'orange'] #user-defined class class Bio: def __init__(self, name, age): self.name = name self.age = age # list of objects my_list = list((Bio("Linda",32), Bio("Mandi",23), Bio("John",24), Bio("Lisa",36), Bio("Jason",40))) # for loop to print object list for l in my_list: print(l.name, l.age, sep = ",") # Linda,32 # Mandi,23 # John,24 # Lisa,36 # Jason,40
In the examples above, we parsed a tuple to the list constructor, which returned a list of integers, strings and user-defined objects.
Attributes of Python List
Below are the attributes of Python lists:
- A list is an ordered collection of elements, where each element has a specific index starting from 0.
- Lists are mutable, meaning you can add, remove, or modify elements after creating the list.
- The elements in a list can be accessed by their index values.
- The indexes of a list are always integers.
- A list can hold duplicate values.
Python Dictionary
A Python dictionary is used to store data in key-value pairs. Let’s see some examples below:
# example 1 my_dict = {"a":1,"b":2,"c":3,"d":4,"e":5} print(my_dict) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} # example 2 player_dict = {"Name": "Thomas Mueller", "Age": 33,"Club": "FC Bayern Munich", "Shirt number": 25} print(player_dict) # {'Name': 'Thomas Mueller', 'Age': 33, 'Club': 'FC Bayern Munich', 'Shirt number': 25}
We can parse an array as dictionary values as well:
items_dict = {"objects":["ball", "cup", "stapler", "pen", "hat"], "owners":["James", "Linda", "Harry", "Tina", "Tommy"], "sex":["male", "female", "male", "female", "male"]} print(items_dict) # {'objects': ['ball', 'cup', 'stapler', 'pen', 'hat'], # 'owners': ['James', 'Linda', 'Harry', 'Tina', 'Tommy'], # 'sex': ['male', 'female', 'male', 'female', 'male']}
We can also use the dict() function to create a dictionary in Python:
fruits_dict = dict(apples=5,oranges=6,bananas=4,pear=2,peach=1) print(fruits_dict) # {'apples': 5, 'oranges': 6, 'bananas': 4, 'pear': 2, 'peach': 1}
The dict()
function takes a key-value pair, as demonstrated above where we parsed apple = 5
to the function, with “apple” being the key, and “5” being the value.
Attributes of Python Dictionary
Below are the attributes of Python dictionaries:
- A dictionary is an unordered collection of key-value pairs.
- Dictionaries are mutable, meaning you can add, remove or modify key-value pairs after creating the dictionary.
- Dictionary values are accessed by their keys.
- The keys of a dictionary are always unique and immutable.
- A dictionary cannot hold duplicate keys. If a key is duplicated, the value associated with the latest occurrence of the key will be used.
So, while lists and dictionaries have a number of fundamental differences, they are both powerful tools for building robust solutions in the Python programming language.
Therefore, I hope this short tutorial has helped you to understand the basic differences between a list and a dictionary in Python.
Video, Further Resources & Summary
Do you need more explanations on the differences between a list and a dictionary in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail the differences between a list and a dictionary in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Convert Python List to PyTorch Tensor & Vice-Versa in Python (Examples)
- Convert List from Boolean to Float in Python (2 Examples)
- Access Elements in Lists within Dictionary in Python (2 Examples)
- Learn Python Programming
This post has explained the differences between a list and a dictionary in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter