List Built In Methods

 

Built in Methods for List Class to perform certain operations


There are certain operations can be done on Lists and are built in for List Class .


  • append() : append data at the end of exist list.

#program

l1 = [10, 20, 30, 40, 50]
print("l1 : ", l1)
l1.append(
60)
print("l1 : ", l1)

#output

l1 : [10, 20, 30, 40, 50]
l1 : [
10, 20, 30, 40, 50, 60]


  • extend() : add all elements of a list to other list.

#program

l1 = [10, 20, 30, 40, 50]
l2 = [
60, 70, 80, 90]
print("l1 : ", l1)
print("l2 : ", l2)
l1.extend(l2)
print("l1 : ", l1)
print("l2 : ", l2)


#output

l1 : [10, 20, 30, 40, 50]
l2 : [
60, 70, 80, 90]
l1 : [
10, 20, 30, 40, 50, 60, 70, 80, 90]
l2 : [
60, 70, 80, 90]


  • insert() : insert new item/member at given index.

#program

l1 = [10, 20, 30, 40, 50]
print("l1 : ", l1)
l1.insert(
2, 60)
print("l1 : ", l1)

#output
l1 : [10, 20, 30, 40, 50]
l1 : [
10, 20, 60, 30, 40, 50]


  • remove() : remove item from list (first occurrence).

#program

l1 = [10, 20, 60, 30, 40, 50, 60]
print("l1 : ", l1)
l1.remove(
60)
print("l1 : ", l1)

#output
l1 : [10, 20, 60, 30, 40, 50, 60]
l1 : [
10, 20, 30, 40, 50, 60]


  • pop() : remove and return element at given index.

#program

l1 = [10, 20, 60, 30, 40, 50, 60]
print("l1 : ", l1)
value = l1.pop(
2)
print("value : ", value)
print("l1 : ", l1)

#output

l1 : [10, 20, 60, 30, 40, 50, 60]
value :
60
l1 : [10, 20, 30, 40, 50, 60]


  • clear() : remove all items from the given list.
 
#program

l1 = [10, 20, 60, 30, 40, 50, 60]
print("l1 : ", l1)
l1.clear()
print("l1 : ", l1)

#output
l1 : [10, 20, 60, 30, 40, 50, 60]
l1 : []


  • index() : return index of 1st match item.

#program

l1 = [10, 20, 60, 30, 40, 50, 60]
print("l1 : ", l1)
found = l1.index(
60)
print("found at :", found)

not_found = l1.index(
70)        #will throw error if value not found
print (not_found, "not found")

print("l1 : ", l1)

#output

l1 : [10, 20, 60, 30, 40, 50, 60]
found at :
2
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 8, in <module>
not_found = l1.index(70)
ValueError: 70 is not in list



  • sort() : sort list items in ascending order.

#program

l1 = [10, 40, 60, 30, 20, 50, 60]
print("l1 : ", l1)
l1.sort()
print("l1 : ", l1)

#output

l1 : [10, 40, 60, 30, 20, 50, 60]
l1 : [
10, 20, 30, 40, 50, 60, 60]


  • reverse() : reverse the order of item in given list

#program

l1 = [10, 40, 60, 30, 20, 50, 60]
print("l1 : ", l1)
l1.reverse()
print("l1 : ", l1)

#output

l1 : [10, 40, 60, 30, 20, 50, 60]
l1 : [
60, 50, 20, 30, 60, 40, 10]


  • copy() : return shallow copy of the list.
 
#program

l1 = [10, 40, 60, 30, 20, 50, 60]
print("l1 : ", l1)
l2 = l1.copy()
print("l2 : ", l2)

#output

l1 : [10, 40, 60, 30, 20, 50, 60]
l2 : [
10, 40, 60, 30, 20, 50, 60]


  • count() : return the number of elements with specified values

#program

l1 = [10, 40, 60, 30, 20, 50, 60]
print("l1 : ", l1)
count = l1.count(
60)
print("count", count)
print("l1 : ", l1)

#output

l1 : [10, 40, 60, 30, 20, 50, 60]
count
2
l1 : [10, 40, 60, 30, 20, 50, 60]




Example on few Python Built in Methods on LIST 


#program

l1 = [10, 40, 60, 30, 20, 50, 60]
print("l1 : ", l1)
print("sum(l1) : ", sum(l1)) # will return sum of all elements in List
print("max(l1) : ", max(l1)) # will return maximum value in list
print("min(l1) : ", min(l1)) # will return minimum value in List
print("len(l1) : ", len(l1)) # will retunn total members count in List

#output
l1 : [10, 40, 60, 30, 20, 50, 60]
sum(l1) :
270
max(l1) : 60
min(l1) : 10
len(l1) : 7









Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India