0% found this document useful (0 votes)
2 views13 pages

Python 12

The document outlines various built-in methods for manipulating lists in Python, including append(), insert(), extend(), copy(), pop(), remove(), clear(), index(), sort(), count(), and reverse(). Each method is described with its functionality, parameters, and examples to illustrate its usage. Additionally, it covers functions like len(), range(), list(), max(), and min() that are useful for list operations.

Uploaded by

harinipb03
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)
2 views13 pages

Python 12

The document outlines various built-in methods for manipulating lists in Python, including append(), insert(), extend(), copy(), pop(), remove(), clear(), index(), sort(), count(), and reverse(). Each method is described with its functionality, parameters, and examples to illustrate its usage. Additionally, it covers functions like len(), range(), list(), max(), and min() that are useful for list operations.

Uploaded by

harinipb03
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

Built-in methods

1. append() – LIST_OBJECT.append(item)
 This method inserts the dataitem (item) at the end of
the existing list.
 LIST_OBJECT must be already created list. “item”
can be a integer / string / float / bool / list / tuple
 There must be only one parameter
Example :
1. A = [ 2, True ]; sum = 35.53
[Link](“Good”) # [ 2, True, “Good”]
[Link](sum) # [ 2, True, “Good”,35.53]
[Link]([8, -67])
# [2, True, “Good”, 35.53, [8, -67]]
2. A = []
[Link](range(5) ) # [ range(0, 5) ]
[Link]( list(range(5)))
# [ range(0,5), [0, 1, 2, 3, 4] ]
3. A = [ ]
[Link](list(“VID”) ) # [ ‘V’, ‘I’, ‘D’]
N = int(input(“Enter a integer “) ) # If input is 25
[Link](N) # [ ‘V’, ‘I’, ‘D’, 25]
4. A = [ False ]
B = [Link]( True )
print(A, B ) # [ False, True]
2. Insert()
 LIST_OBJECT.insert(indexno, dataitem)
 This method is used to insert a dataitem at a
specified index position in an existing list
 It has two arguments. The first one is the index
number and the second one is the value that has to
be inserted.
 When a data item is inserted into a list, the list is
expanded in size and length to accommodate the
new data item
 The data that was previously at the specified index
and the items after that index are shifted one
position to the right of the list.
 NO ERROR will be flashed when an index number
is specified out of range.
 If the index is beyond the end of the list, it is added
to the end of the list (it is appended/ added at the
end of the list)
 If a negative index is specified, then the data is
added at the beginning of the list
Examples :
a. A = [1, 2, 3]
[Link](2, 67)
print( A ) # [ 1, 2, 67, 3 ]
b. A = [1, 2, 3]
[Link](0, 67)
print( A ) # [ 67, 1, 2, 3 ]
c. A = [1, 2, 3]
[Link](20, 67)
print( A ) # [ 1, 2, 3, 67 ]
d. A = [1, 2, 3]
[Link](-25, “Good”)
print( A ) # [ “Good”, 1, 2, 3 ]
e. A = [1, 2, 3]
[Link](1, [True, 67, 8.9] )
print( A ) # [ 1, [True, 67, 8.9] 2, 3 ]
f. A = [1, 2, 3]
[Link](0, list(“CAT”))
print( A ) # [ [ ‘C’, ‘A’, ‘T’] , 1, 2, 3 ]
g. A = [1, 2, 3]
B = [Link](0, list(range(3)))
# both A & B is modified
print( A ) # [ [ 0, 1, 2 ] , 1, 2, 3 ]
print( B ) # [ [ 0, 1, 2 ] , 1, 2, 3 ]

3. extend () :
 This function is used to add a new list into an
already existing list.
 LIST_OBJECT.extend( Iterable_Data )
 There must be only one parameter which must be a
iterable one. Ie., it can be string / list / tuple / set
 While using extend() function it cannot be assigned
to another variable.
 Examples :
A = [ 1, 3]; B = “God”;
C = [True, “CAT”]
[Link](B) # Only A is extended.
print(A) # [ 1, 3, ‘G’,’o’,’d’]
print(B) # “God”
[Link]( C ) # [1, 3, ‘G’,’o’,’d’, True,”CAT” ]
[Link](56) # Error
[Link](True) # Error
[Link]() #Error
[Link](B, C) #Error
[Link](“Dog”) # [ True, “CAT”, ‘D’,’o’,’g’]
[Link](“ “) # [ True, “CAT”, ‘D’,’o’,’g’, ‘ ‘]
[Link](““) # [ True, “CAT”, ‘D’,’o’,’g’, ‘ ‘]
L=[]
[Link]( [56] ) # [ 56 ]
[Link]( “AB” ) # [ 56, ‘A’,’B’ ]
[Link]( [“CD”] ) # [56, ‘A’, ‘B’, “CD”]
[Link]( list(“EF”) )
# [56, ‘A’, ‘B’, “CD”, ‘E’, ‘F’]
[Link]( range(5) )
# [56, ‘A’, ‘B’, “CD”, ‘E’, ‘F’, 0, 1, 2, 3, 4]
4. copy () :
 LIST_OBJECT.copy()
 This function returns a shallow copy of a list.
 Its called shallow copy because the list to which it is
copied is independent of the original list

 Assume L1 is a list. Then L2=L1 makes L2 and L1


to point the same list. So when L1 is modified, L2 is
also getting modified. Similarly when L2 is
modified, L1 is also modified. But this does not
happened when a list is copied using copy()
method.

Assume L1 = [ 25, True, “Vidya”]


When L2 = L1 is given
25 True Vidya
L1 L2

When L2 = [Link]( ) is given


25 True Vidya
L1
25 True Vidya
L2

 Example :
L1 = [ 2, 4, 6 ]
L2 = L1
L3 = [Link]()
[Link]( )
[Link](8)
[Link](100)
print(L1) # [2, 4 , 100]
print(L2) # [2, 4, 100]
print(L3) # [2, 4, 6, 8]
Note : The deletion of data from the list can be done using
the methods pop(), remove(), clear() or using a
statement del. The elements are automatically shifted to
the left.
1. pop() is used when index is known
2. remove() is used when the data item is known
3. del is a statement used when multiple data items are to
be deleted. (Its explained in the later part of this
document)
5. pop(index) :
 This function is used to remove the data item
specified in the index number of an existing list.
 It returns the item that is removed from the list.
 LIST_OBJECT. pop( index )
 If the index is not specified then it removs the last
dataitem from the LIST
 If the list is empty, or if the index is outside the
range, we get an error message
 Examples :
A = [ 1, True, “good”]
[Link]()
print(A) # [ 1, True ]
[Link](1 )
print(A) #[1]
[Link](3) # Error
6. remove() :
 This function is used to delete a data item from an
existing list, if the dataitem is present in the list.
 LIST_OBJECT.remove( dataitem)
 If the specified is not in the list then it flashes an
error.
 If the data item is available more than once, then the
first occurrence of the data is removed.
 Example :
A = [1, 2, 3, 4 ]
[Link](3)
print(A) # [1, 2, 4]
[Link](100) # Error
[Link](1, 2) # Error

7. clear() :
 This function is used to clear the contents of the list
and the list becomes empty.
 LIST_OBJECT.clear( )
 It does not have any parameter
 It doesn’t remove the list but only the elements of
the list is cleared
 Example :
L = [ 2, True, “Good”]
[Link]( )
print(L) #[]
8. index () :
 This function returns the index number of the first
occurance of the data item/substring in the
list/string.
 If the data is not found then it flashes an error.
 LIST_OBJECT.index(dataitem, start_index, end_index)
 This searches the dataitem in the LIST_OBJECT from the
Start_index to end_index-1.
 Example :
A = [2, 4, 6, 8, 10, 2, 4]
[Link]( 6 ) #2
[Link]( 8, 0, 2) # Error
[Link]( 8 ) #3
[Link]( 2 ) #0
[Link]( 2, 3, 6) # 5
S = “abcabcabc”
[Link](“ab”) #0
[Link](“ab”, 5, 8 ) # 6
[Link](“bc”, 5, 8) # 7

9. sort() :
 This method is used to sort the contents of the list in
ascending order / descending order / alphabetical
order / reverse alphabetical order.
 LIST_OBJECT.sort( key = none, reverse = False)
 While using sort() function, the entire list must be
same data type. Otherwise, it flashes an error.
 By default, it sorts in alphabetical / ascending order.
If it needs to be sorted in reversed alphabetical
order or descending order then the reverse = True/1
has to be given as an argument.

 The argument key can be a built-in function or a


predefined functions like len, chr, ord, str, abs etc
key function - where each argument is passed, and comparison is
performed based on its return value.
 It sorts the contents and stores it back in the same
list itself.
 Example :
A = [ 2, -8, 5 ]
[Link]( )

print(A) # [ -8, 2, 5 ]
[Link](reverse = True )
print(A) # [ 5, 2, -8 ]
 A = [ 2, -8, 5 ]
[Link]( key = abs) # sorts the absolute values
print(A) # [ 2, 5, -8 ]
 S = [ “Crow”, “Fly” , “ Peacock”]
[Link]()
print( A ) # [“ Peacock” , “Fly”, “Crow”]
[Link]( key = len )
print(A) # [ “Fly”, “Crow”, “ Peacock”]
 B = [ 23, True, 9, 2 ]
[Link]() # [ True, 2, 9, 23]
10. count() :
 This function is used for list / string.
 This function is used to count and returns number of
times a substring appears in a string or to count
and returns the number of dataitems present in the
list
 LIST_OBJECT.count(dataitem,start, end) . This
syntax is for list object
 [Link]( substring, start_index, end_index).
This syntax is for string.
 In case of string, two or three parameter can be
used. If the third parameter is not given then it
counts till the last character of the string.
 If zero parameter / morethan 3 parameter, it flashes
an Error.
 Example :
 A = [1, 2, 3, 4 , 1]
print( [Link]( 3 ) ) #1
print([Link]( 1 ) ) #2
print([Link](100)) #0
print([Link]( ) ) # Error
print([Link]( 3, 0, 2 ) ) #
 S = “Butterfly and Dragonfly”
print([Link](‘fly’) ) #2
print([Link](‘man’) ) #0
print([Link](‘fly’, 0, 10 ) ) # 1
print([Link](‘ ‘)) #2
11. reverse() :
 This function is used to reverse the contents of the
list and stores the reversed list in the same list itself.
Original list is lost.
 LIST_OBJECT.reverse()
 No parameter to be given
 Example :
A = [ 2, “good”, True ]
[Link]( )
print(A) # [True, “good”, 2 ]
 B. [ [10, 20, 30], [“good”, “bad” ] ]
[Link]( )
print( B) # [ [ “good”, “bad”] , [ 10, 20, 30 ]]
Functions
1. len() -
 This function returns the length of the string/list.
 It takes only one parameter
 len( LIST_OBJECT / String )
 A = [ 34, “True”, “Vidya”]
S = “VidyaMandir”
Print(len(A), len(S) ) # 3 11
2. range() – This creates a list of immutable data from start to
end-1 with step.

3. list() –
 This function creates a list of iterable values.
 list( string / range)
 Example :
A = list() # A will be [ ]
A = list(“Good”) # A will be [‘G’, ‘o’, ‘o’, ‘d’ ]
A = list( range(5) ) # A will be [ 0, 1, 2, 3, 4]
A = list( range( 5, 0, -1 ) # A will be [ 5, 4, 3, 2, 1 ]
A = list ( 5 ) # Error – 5 is not iterable
4. max ()
 This returns the largest data in the iterable list / tuple /
string.
 max( ITERABLE_OBJECT , default = object, key = function )
 The iterable set of values must be same datatype.
Otherwise it flashes an error.
 The parameter cannot be integer / float / bool
5. min ()
 This returns the smallest data in the iterable list / tuple
/ string.
 min( ITERABLE_OBJECT , default = object, key = function )
 The iterable set of values must be same datatype.
Otherwise it flashes an error.
 The parameter cannot be integer/float/bool
 Example :
A = [ -23, 94, 100, 35, 29, 10987]
print(max(A) ) # 10987
print( min(A) ) # -23
C = [ 455, True, “Good”]
print(max ( C ) ) # Error
 d = [ "abc", "pqr", "xyz", "abcde","ab"]
print( max(d) ) # ‘xyz’
print( min(d) ) # ‘ab’
 d = [ "abc", "abcde","ab"]
print( max(d) ) # ‘abcde’
print( min(d) ) # ‘ab’
 d = [ "Computer", "Zebra","Aeroplane"]
print( max(d) ) # ‘Zebra’
print( min(d) ) # ‘Aeroplane’
print( max(d, key = len) ) # ‘Aeroplane’
print( min(d, key = len) ) # ‘Zebra’
 A = [ -23, -100, -25]
print(min(A)) # -100
print(min(A, key = abs) # -23

6. del
 This is a statement (Not a function /method ) used to
remove the data given in the specified list index
 Del can be used with slicing also. So that it removes all
the data in the specified slice and all the data are shifted
to the left.
 Example :
A = [ 2, 4, 6, 8, 10]
del A[2]
print(A) # [2, 4, 8, 10]
del A[0:2]
print(A) # [ 8, 10 ]

A = [ 2,4,6,8,10]
del A[1], A[3]
print(A) # [2, 6, 8 ] # after deleting A[1], A[3] will get deleted
7. sorted ()
 This function returns a sorted list of values.
 The parameter must be iterable (string/list/tuple),
not int/float/bool
 sorted( Iterable_Object, key = none, reverse = True/False)
 Example :
1. A = sorted( “Good”)
print(A) # [ ‘G’, ‘d’, ‘o’, ‘o’]
2. d = [ "Computer", "Zebra","Aeroplane"]
print( sorted(d ))
# [“Aeroplane”, “Computer”,“Zebra”]
3. d = [ "Computer", "Zebra","Aeroplane"]
print( sorted(d, key = len ))
# [“Zebra”, “Computer”, “Aeroplane” ]
4. d = [ "Computer", "Zebra","Aeroplane"]
print( sorted(d, key = len , reverse = True) )
# [ “Aeroplane” , “Computer”, “Zebra” ]
5. E = [ 23, -9, 20, 1.6, 23 ]
print( sorted(E) ) # [ -9, 1.6, 20, 23, 23 ]
6. N = 67
sorted( N) # Error

You might also like