An Introduction to Python List

Let’s learn about python list in detail

Indhumathy Chelliah
The Startup

--

Photo by Paulo Jacobe on Unsplash

The list is a data structure in Python which contains a mutable, ordered sequence of elements.

Creating a list:

l1 = [1, 2, 3]
l2 = [‘apple’, ‘banana’, ‘orange’, ‘grapes’, ‘mango’]

List constructor: list()
List constructor takes sequence types and converts to list.It is used to convert tuple to list.
t1=(1,2,3)
l1=list(t1)
print (l1)
Output:
[1, 2, 3]

Accessing elements from list:
List indices start from 0.

l1[0] -> 1 (first element in list l1)
l2[2] -> orange (third element in list l2)
l1[-1] -> 3 (Negative index -1 means starting from the end of the list)
l1[-2] -> 2

List Slicing:
We can specify range of indexes. start and stop. stop index is not included.

l1=[0,1,2,3,4,5]
print (l1[1:4])
Output:[1, 2, 3]

Updating List:
Inserting elements into the list. insert method will insert elements…

--

--