What is a List in Python and why we need it?

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,

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top