Python List with Example – Python Tutorial
6 years ago Lalit Bhagtani 0
In Python list are a sequence of objects just like a string. Unlike strings, list is a mutable data structure, which means that data in the list can be changed or replaced.
In this tutorial, we will learn about the following:
- Creating List
- List Indexing
- List Slicing
- Nesting List
- Update & Delete List Element
Creating List
In Python list are created by enclosing sequence of elements separated by commas inside brackets “[ ]“. For example:
# creating list with same data type
l = [1,2,3,4,5]
# creating list with different data type
l = [1,'Jan',2.0,True]
# creating empty list
l = []
List Indexing
In python list are a sequence just like a string, So we can use indexes to access individual element of the list. Index starts from 0 and brackets [] are used after an object variable to access the element. For example:
# create list object and assign it to variable l
l = [1,2,3,4,5]
# accessing element at index 0
print(l[0])
# accessing element at index 1
print(l[1])
# accessing last element of the list
print(l[2])
# another way of accessing last element of the list
print(l[-1])
Like string, negative numbers can also be used for indexing. Indexing diagram for list [‘a’,’b’,’c’,’d’] as follows:
a b c d
0 1 2 3
-4 -3 -2 -1
List Slicing
In python list can be sliced by using colon “:” operator inside brackets “[]“.
Syntax : [ start index : end index : step ]
start index : slice of the given list will starts from this index, including element at start index
end index : slice of the given list will end at this index, excluding element at end index
step : it is a frequency of grabbing elements from the given list
For example :
# create list object and assign it to variable l
l = ('a','b','c','d')
print(l[1:])
# This prints a slice of the list, starting from index 1 and ending at last index
print(l[:1])
# This prints a slice of the list, starting from index 0 and ending at index 1
print(l[1:2])
# This prints a slice of the list, starting from index 1 and ending at index 2
print(l[::2])
# This prints a slice of the list, starting from index 0 and ending at last index and grab every alternate element only
# print complete list in reverse
print(l[::-1])
Nesting List
In python list can be created inside another list. This can be used to create matrix data structure. For example:
# create three list object and assign it to variable l1, l2, l3
l1 = [1,2,3,4]
l2 = [5,6,7,8]
l3 = [9,10,11,12]
# create a list of list to make a matrix
m = [l1,l2,l3]
# print 2nd list
print(m[1])
# print 1st element of 3rd list
print(m[2][0])
Update & Delete List Element
Updating list element is very simple, just use assignment operator to assign new value to position of the element, which you want to update.
For deleting list element or list object entirely, we can use del statement. For example:
# create list object and assign it to variable l
l = [1,2,3,4,5,6,7,8]
# print all elements of list
print(l)
# update element at index 1
l[1] = 9
# print all elements of list
print(l)
# delete element at index 2
del l[2]
# check list elements after deletion
print(l)
# delete element from index 4 to 7
del l[4:7]
# check list elements after deletion
print(l)
# delete list
del l
# check list
print(l)
References :-
That’s all for Python List with Example. If you liked it, please share your thoughts in comments section and share it with others too.