Member-only story
A Complete Guide to Python List
The Properties of Python List
In Python, one of the most used data structures is list
. A list
in Python is basically an ordered collection that can hold a number of items or objects. Just think about a to-do list, which basically just contains the list of things for us to do.
List initialisation
There are 2 ways to initialise a list
object.
demo_one = list([1, 2, 3, 4, 5])
print(demo_one) # [1, 2, 3, 4, 5]demo_two = [1, 2, 3, 4, 5]
print(demo_two) # [1, 2, 3, 4, 5]
Alternatively, if you don’t already know what are the items the list will store, you can also initialise an empty list
like so.
empty_one = list()
print(empty_one) # []empty_two = []
print(empty_two) # []
List index
The position of each element in a list
is indexed. The index of a list
begins at 0
.
some_list = [1, 2, 3, 4]print(some_list[0]) # 1
print(some_list[1]) # 2
print(some_list[2]) # 3
print(some_list[3]) # 4
print(some_list[-1]) # 4
Note that we can refer to the index of the last element of a list
as -1
.