Professional Documents
Culture Documents
List methods:
Method Example Description
sum() sum(lst) Returns sum of all elements in the
list
index() list.index(x) Returns first occurrence of x in the
list
append() list.append(x) Appends x at the end of the list
insert() list.insert(i,x) Inserts x into the list in the position
specified by I
copy() list.copy() Copies all the list elements into a
new list and returns it
extend() list.extend(list1) Appends list1 to list
count() list.count(x) Returns number of occurrence of x
in the list
remove() list.remove(x) Removes x from the list
pop() list.pop() Removes the ending element from
the list
sort() list.sort() Sorts the elements of the list into
ascending order
Method Example Description
Reverse() list.reverse() Reverse the sequence of elements
into the list
Clear() list.clear() Deletes all elements from the list
Example program on List methods
num=[1,2,3,4,5]
print(sum(num))
print('Index of 2 is',(num.index(2)+1))
num.append(6)
print('After appending 6',num)
num.insert(0,7)
print('After appending 7 at 0 th position',num)
num[0]=8
print(num)
num1=num.copy()
print('Newly Created List',num1)
num.extend(num1)
print('Num after appending num1',num)
print('No. of times 5 found in the list',num.count(5))
num.remove(5)
print('list after removing 5',num)
num.pop()
print('After removing ending element',num)
num.sort()
print('List after sorting',num)
num.reverse()
print('List after reversing',num)
num.clear()
print('List after removing all elements',num)
num=[10,20,30,40,50]
print('Biggest element in list',max(num))
print('Smallest element in list',min(num))
Nested List:
A list within another list is called as Nested List. When we take a list as an
element in another list, then the list is called a Nested List.
For Example:
We have two list a and b as:
a=[80,90]
b=[10,20,30,a]
print(b) #[10,20,30,[80,90]]
The element of b appears:
[10,20,30,[80,90]]
The last element [80,90] represents a nested list. So ‘b’ has 4 elements
and they are:
b[0]=10
b[1]=20
b[2]=30
b[3]=[80,90]
List Comprehensions:
List comprehensions represent creation of new lists from an iterable
object(like a list, set, tuple, dictionary or range) that satisfy a given
condition.
List comprehension contain very compact code usually a single
statement that performs a task.
Example 1:
We want to create a list with sequence of integers from 1 to 10. We
can write code as:
squares=[]
for x in range(1,11):
squares.append(x**2)
Output: [1,4,9,16,25,36,49,64,81,100]
The preceding code will create ‘squares’ list with the elements as
shown below:
squares=[x**2 for x in range(1,11)]
Since strings and tuples are immutable, their hash value remains same at
all times. Hence a set of strings or tuples is permitted. However, a list
may change, so its hash value may change, hence set of lists are not
permitted.
s={15,25,35,45,55}
print(s) #prints {15,25,35,45,55}
Note that unlike a string, list or tuple, a while loop should not be used to
access the set elements. This is because we cannot access a set element
using an index, as in s[i].
Basic Set Operations:
Sets are like mutable. Their contents can be changed.
s={‘gate’,’fate’,’late’}
s.add(‘rate’) # adds one more elements to set s