Member-only story

A Complete Guide to Python List

billydharmawan
The Startup
Published in
8 min readMay 6, 2020
Photo by Emma Matthews Digital Content Production on Unsplash

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.

List can hold different data…

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

billydharmawan
billydharmawan

Written by billydharmawan

An ordinary man trying to leave a good legacy on earth

No responses yet