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.
print(list_of_ints)
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.
- etc.
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)
Output
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)
Output
Size of list: 0
Summary:
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
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
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.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.