0% found this document useful (0 votes)
3 views

List

Class 12 Computer notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

List

Class 12 Computer notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

REVISION TOUR LISTS

 List is a collection of elements which is ordered


 It is represented by [ ]
 Strings are mutable in python. It means it is changeable.

EXAMPLE
Forward indexing

0 1 2 3 4 5 6 7
7 5 9 4 2 1 3 8
-8 -7 -6 -5 -4 -3 -2 -1

Backword Indexing

CREATING A LIST
 n=[7,5,9,4,2,1,3,8]
 n=[ ]
 n=list()

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[2]) 9

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[-2]) 3

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[2:6]) [9, 4, 2, 1]

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[0:7:2]) [7, 9, 2, 3]

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[2:]) [9, 4, 2, 1, 3, 8]

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[:5]) [7, 5, 9, 4, 2]
n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[-5:-1]) [4, 2, 1, 3]

n=[7,5,9,4,2,1,3,8] OUTPUT
print(n[::-1]) [8, 3, 1, 2, 4, 9, 5, 7]

a=[2,4,6] OUTPUT
b=[3,8,9] [2, 4, 6, 3, 8, 9]
print(a+b)

a=[2,4,6] OUTPUT
print(a*2) [2, 4, 6, 2, 4, 6]

LIST RELATED FUNCTIONS


FUNCTION EXAMPLE OUTPUT

append( ) To add element to the list at the end [2, 4, 6, 8, 100]


n=[2,4,6,8]
n.append(100)
print(n)
extend( ) Add a list, to the end of the current list. [2, 4, 6, 8, 100, 200]
n=[2,4,6,8]
m=[100,200]
n.extend(m)
print(n)
insert( ) Adds an element at the specified position [2, 4, 'abc', 6, 8]
n=[2,4,6,8]
n.insert(2,"abc")
print(n)
remove( ) To remove an element from the list. [2, 4, 8]
n=[2,4,6,8]
n.remove(6)
print(n)
pop( ) To remove last element from list [2, 4, 6]
n=[2,4,6,8]
n.pop()
print(n)
pop( index) To remove element from given index [2, 4, 8]
n=[2,4,6,8]

n.pop(2)

print(n)

clear( ) Removes all the elements from list. []


n=[2,4,6,8]
n.clear()
print(n)
len( ) Find the length of the list. 4
n=[2,4,6,8]
print(len(n))
index( ) Returns the index of the first element with the 2
specified value.
n=[2,4,6,8]
i=n.index(6)
print(i)

count( ) Return the number of times the value appears. 2


n=[2,40,6,8,40]
c=n.count(40)
print(c)
sort( ) To sort content of list in ascending order [2, 4, 5, 7, 9]
n=[7,5,9,2,4]
n.sort()
print(n)
sort(reverse=True ) To sort content of list in descending order [9, 7, 5, 4, 2]
n=[7,5,9,2,4]
n.sort(reverse=True)
print(n)
reverse( ) Reverses the order of the list. [4, 2, 9, 5, 7]

n=[7,5,9,2,4]
n.reverse()
print(n)
del list[i] Removes element of list at particular index [7, 5, 2, 4, 1, 3]
n=[7,5,9,2,4,1,3]
del n[2]
print(n)
del list[n:m] Removes element of list in the range [7, 5, 1, 3]
n=[7,5,9,2,4,1,3]
del n[2:5]
print(n)

Write a function square (L) where L is list passed as argument to the function the
function returns another list m that stores squares of non zero elements
If L contains L=[9,4,0,11,0,6,0]
m will have [81, 16, 121, 36]

def square(L):
m=[]
for z in L:
if z!=0:
m.append(z*z)
return(m)
L=[9,4,0,11,0,6,0]
m=square(L)
print(m)

OUTPUT:- [81, 16, 121, 36]

You might also like