List
Common List Operations
Method name
Description
x in s
True if element x is in sequence s, False otherwise
x not in s
True if element x is not in sequence s, False otherwise
s1 + s2
Concatenates two sequences s1 and s2
s * n , n * s
n copies of sequence s concatenated
s[i]
ith element in sequence s.
len(s)
Length of sequences, i.e. the number of elements ins`.
min(s)
Smallest element in sequence s.
max(s)
Largest element in sequence s.
sum(s)
Sum of all numbers in sequence s.
for loop
Traverses elements from left to right in a for loop.
List examples using functions
>>> list1 = [2, 3, 4, 1, 32]
>>> 2 in list1
# True
>>> 33 not in list1
# True
>>> len(list1) # find the number of elements in the list
# 5
>>> max(list1) # find the largest element in the list
# 32
>>> min(list1) # find the smallest element in the list
# 1
>>> sum(list1) # sum of elements in the list
# 42List slicing
+ and * operators in list
+ and * operators in listin or not in operator
in or not in operatorTraversing list using for loop
List Comprehension
Commonly used list methods with return type
Method name
Description
append(x:object):None
Adds an element x to the end of the list and returns None.
count(x:object):int
Returns the number of times element x appears in the list.
extend(l:list):None
Appends all the elements in l to the list and returns None.
index(x: object):int
Returns the index of the first occurrence of element x in the list
insert(index: int, x: object):None
Inserts an element x at a given index. Note that the first element in the list has index 0 and returns None.
remove(x:object):None
Removes the first occurrence of element x from the list and returns None
reverse():None
Reverse the list and returns None
sort(): None
Sorts the elements in the list in ascending order and returns None.
pop(i): object
Removes the element at the given position and returns it. The parameter i is optional. If it is not specified, pop() removes and returns the last element in the list.
Last updated
Was this helpful?