Create a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1)
print(type(list1))
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
<class 'list'>

Create an empty list

list1 = []
print(list1)
print(type(list1))
Output:
[]
<class 'list'>

Access an element in a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1[0])
Output:
1

Access an element in a list using negative index

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1[-1])
Output:
10

Access a range of elements in a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1[0:5])
Output:
[1, 2, 3, 4, 5]

Add an element to a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.append(11)
print(list1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Add an element to a list at a specific index

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.insert(0, 0)
print(list1)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Remove an element from a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.remove(10)
print(list1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Remove an element from a list at a specific index

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.pop(0)
print(list1)
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10]

Remove an element from a list using del

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del list1[0]
print(list1)
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10]

Remove all elements from a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.clear()
print(list1)
Output:
[]

Check if an element is in a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(11 in list1)
print(1 in list1)
Output:
False
True

Get the length of a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(len(list1))
Output:
10

Sort a list

list1 = [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
list1.sort()
print(list1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Sort a list in reverse order

list1 = [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
list1.sort(reverse=True)
print(list1)
Output:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Reverse a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.reverse()
print(list1)
Output:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Copy a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = list1.copy()
print(list2)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Join two lists

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
list3 = list1 + list2
print(list3)
print(type(list3))
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
<class 'list'>

Join two lists using extend

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
list1.extend(list2)
print(list1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Remove duplicates from a list

Set is a collection of unique elements. set is used to remove duplicates from a list.

list1 = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10]
list1 = list(set(list1))
print(list1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Find the index of an element in a list

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1.index(5))
Output:
4

Was this page helpful?