This article will answer questions like what a list is in Python and why we need a list data structure in Python.
What is a List?
The list is a sequential data structure in Python. It can contain a group of elements, which can be of the same or different data types.
A basic example of a list
We can create a list object using square brackets, i.e. . For example, to create a list of integers, enclose them in square brackets. Like this,
list_of_ints = [11, 13, 26, 90, 5, 22]
Here we created a list of integers. We can directly print the list object on the console using the print() function i.e.
If we don’t pass any values in the square brackets, it will create an empty list. For example,
[11, 13, 26, 90, 5, 22]
Why do we need a list in Python?
A list helps to store a group of elements together. But many other sequential data structures do the same, like a set, tuple, etc. Then why do we need lists?
There are many unique properties of the list that make it different from other data types. It makes a list of one of the most used sequential data types in Python. Let’s explore these properties one by one,
- Lists are Dynamic
- A list is dynamic; it means we can add multiple elements to it and update or delete elements. Also, we don’t need to predefine the size of the list. You can insert any number of items in the list, and it can dynamically increase its size internally.
- Lists are ordered
- Elements in the list will remain in the order in which you add them, and it will not change the order of elements internally.
- Lists are Heterogenous
- A list can contain elements of different data types. For example, in a single list object, you can keep integers, floats, strings, tuples, lists, and other things too.
- Duplicates are allowed in a list
- A list in Python can contain duplicate elements.
So, wherever you are looking for a heterogeneous data structure that can dynamically change its size, keep elements ordered, and contain duplicates. Then a list is a perfect choice.
Some of the use-cases where we generally use lists are,
- List of active players in the game.
- List of items in the shopping cart.
- List of the running processes in the system.
- List of messages received by the server.
Get the size of a list in Python.
To get the list size, we can pass the list object to the len() function. It will return the length of the list. For example,
list_of_ints = [11, 13, 26, 90, 5, 22] # Get size of list length = len(list_of_ints) print('Size of list: ', length)
Size of list: 6
If a list is empty, then the size of the list will be 0. For example,
# Create empty List list_of_ints =  # Get size of list length = len(list_of_ints) print('Size of list: ', length)
Size of list: 0
Today we learned about the need for list data structures in Python. In the upcoming articles, we will discuss how to perform various operations on a list in Python.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.