Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python List


List

A list stores multiple items in a single variable.

You can create a list by using square ([]) brackets.

Example
numbers_list = [
    0, 1, 2, 3, 4, 5
]
print(numbers_list)
[0, 1, 2, 3, 4, 5]

In the above example, we declared a list of items from 0 to 5 using a square bracket ([]) and printing that list using the print() function.


A List Allows Duplicate Items

There is no restriction on adding the same items to the list more than once.

Example
numbers_list = [
    0, 1, 1, 2, 2, 2
]
print(numbers_list)
[0, 1, 1, 2, 2, 2]

In the above example, we added integers 1 and 2 multiple times to the list.


Python List Items Data Types

There is no restriction on adding similar data type items into the list, you can add different data types as well.

Example
list1 = [0, 1, 2]
list2 = ["Red", "Blue", "Green"]
list3 = [True, False, True]
list4 = [1, "Red", True]

print(list1)
print(list2)
print(list3)
print(list4)
[0, 1, 2]
['Red', 'Blue', 'Green']
[True, False, True]
[1, 'Red', True]

In the above example, we created four different lists (list1, list2, list3, and list4) and added different data types of data into the list.

In the first list, we listed integer data type items (list1 = [0, 1, 2]) in the second list, string data type items (list2 = ["Red", "Blue", "Green"]) in the third list, boolean data type items (list3 = [True, False, True]), and in the fourth list, we added a combination of different data type items (list4 = [1, "Red", True]).


Python list() Constructor

You can also create a list using the list() constructor.

Example
numbers_list = list((0, 1, 2, 3, 4, 5))
print(numbers_list)
[0, 1, 2, 3, 4, 5]

In the above example, we use a list() constructor for creating the list.


List Items Count

To determine how many items are in the list, use the len() function.

Example
numbers_list = [
    0, 1, 2, 3, 4, 5
]
print(len(numbers_list))
6

In the above example, the number of items count is 6.