List is used to store multiple values. list is created using square bracket.
l = [10, 20, 30, 40, 50]
List indexing
print(l[0]) # 10
print(l[1]) # 20
List splitting
print(l[0: 3]) # [10, 20, 30]
Change list item
l[0]= 100
print(l) # 100, 20, 30, 40, 50
List methods
count() | Count number of elements of specified value |
l=[1,2,3,1] |
append() | Append new value to list |
l=[1,2,3] |
clear() | Remove all elements from a list |
l=[1,2,3] |
copy() | Return copy of a list |
x=[1,2,3] |
extend() | Add one list elements to other list |
x=[1,2,3] |
index() | Return index of specified item from a list |
x=[1,2,3] |
insert() | Insert new item at specified index |
x=[10, 20, 30] |
pop() | Remove last item from a list |
x=[10, 20, 30] |
reverse() | Reverse a list |
x=[1,2,3] |
remove() | Remove the specified item from a list |
x=[1,2,3] |
sort() | Sort list items in ascending or descending order |
x=[1, 21, 3] |