Skip to content

CRUD Operation in Python




Python – CRUD Operation


As we read earlier about Python list. Now we will study about CRUD Operation, These are neither a method of Python nor any function. It means that these are not a part of Python programming. But if we do this CRUD operation in Python, then we can easily make some python programs. These operations have a lot of advantages for Python programming.





What is CRUD Operation in Python ?

CRUD Operation are those basic and important operation which one should know how to apply after studyingListdata type.


Python-CRUD-Operation





C – CREATE

First, let’s study our first operation of CRUD which is “C” for create which means either to add elements or to insert them.

Here are some functions of CREATE :-


  • append

  • insert

  • extend

As you have studied how to create a list, let us create a list first.

list_1 = [1, 2, 3, 4]



append :

Append is a function of list that add given element at the end of the list.



General syntax for append :

list.append(self, element)

Suppose we want to add an element ” 5 ” at the end of a list, what we can do is to use append( ) function.



Example :

list_1 = [1,2,3,4] 
list_1.append(5) 
print(list_1)



Output :

[1, 2, 3, 4, 5]



You can also append a list.



Example :

list_1 = [1,2,3,4] 
list_2 = [5,6,7,8] 
list_1.append(list_2) 
print(list_1)



Output :

[1, 2, 3, 4, [5, 6, 7, 8]]

As we can see the list_2 just got appended to list_1.





⇒ insert :


Insert is a function of list that add element at given index position.



General syntax for insert :

list.insert(self, index, element)



Example :

list_1 = [1,2,3,4] 
list_1.insert(1,5) 
print(list_1)



Output :

[1, 5, 2, 3, 4]



You can apply insert( ) and append( ) both at a time.



Example :

list_1 = [1,2,3,4] 
list_1.insert(1,[11,22,33,44]) 
list_1.append([55,66,77,88]) 
print(list_1)



Output :

[1, [11, 22, 33, 44], 2, 3, 4, [55, 66, 77, 88]]



You can add element at given index position into a list of list.



Example :

list_1 = [1,2,3,4] 
list_1.insert(1,[11,22,33,44]) 
list_1.append([55,66,77,88]) 
list_1[1].insert(1,7) 
print(list_1)



Output :

[1, [11, 7, 22, 33, 44], 2, 3, 4, [55, 66, 77, 88]]



⇒ extend :

The extend( ) is a function of list that adds the specified list elements ( or any iterable ) to the end of the current list.



General syntax for extend( ) :

list.extend(self, iterable)



Example :

list_1 = [1,2,3,4] 
list_2 = [11,22,33,44] 
list_1.extend(list_2) 
print(list_1)



Output :

[1, 2, 3, 4, 11, 22, 33, 44]



Another way to add the specified list elements to the end of the current list.



Example :

list_1 = [1,2,3,4] 
list_2 = [11,22,33,44] 
list_3 = list_1 + list_2 
print(list_3)



Output :

[1, 2, 3, 4, 11, 22, 33, 44]



You can also add string ( one by one ) to the end of the current list.



Example :

list_1 = [11,22,33,44] 
list_1.extend("abcd") 
print(list_1)



Output :

[11, 22, 33, 44, 'a', 'b', 'c', 'd']



You can also add string to the end of the current list with the help of extend( ) function.



Example :

list_1 = [11,22,33,44] 
list_1.extend(["Black"]) 
print(list_1)



Output :

[11, 22, 33, 44, 'Black']





R – READ

Let’s study our second operation of CRUD which is ” R ” for read which means either to read data or to search data from the list.

Here are some functions of READ :-

  • index

  • _ _contains_ _

  • count

Let us create a list first.

list_1 = [1, 2, 3, 4]



⇒ index :

Index functions search element into list, if found returns index positions of element, otherwise returns error.



General syntax for index( ) :

list.index(self, element, start, end)



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.index(2) 
print(i)



Output :

2



There are three 1’s in list1. You can also search the first “1” from list1 using index( ) functions, if found returns index position of element.



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.index(1) 
print(i)



Output :

0



You can also search the second “1” from list_1 using index( ) functions, if found returns index position of element.



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.index(1,1) 
print(i)



Output :

3



You can also search the third “1” from list+1 using index( ) functions, if found returns index position of element.



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.index(1,4) 
print(i)



Output :

6




⇒ _ _contains_ _ :

_ _contains_ _( ) function which will return boolean value. True means the element exists and False means there is no such element in the list.



General syntax for _ _contains_ _( ) :

list._ _contains_ _(self, element)



Elements are exists in list 1.



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.__contains__(7) 
print(i)



Output :

True



Elements are not exist in list 1.



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.__contains__(10) 
print(i)



Output :

False




count( ) :

count( ) function which will return you the count of the element that you pass in it. count( ) function always return integer data type.



General syntax for count ( ) :

list.count(self, element)



Example :

list_1 = [1,3,2,1,5,7,1,8] 
i=list_1.count(1) 
print(i)



Output :

3







U – UPDATE

Let’s study our third operation of CRUD which is “U” for update operation is used to modify the contents of a list or to make changes in a list.



Example :

list_1 = [11,22,33,44,55] 
list_1[2]=300 
print(list_1)



Output :

[11, 22, 300, 44, 55]





D – DELETE

Let’s study our last operation of CRUD which is “D” for delete operation is used to remove an element from the list.

Here are some functions of DELETE :-

  • remove

  • pop

  • clear

let us create a list first.

list_1 = [1,2,3,4,5]




⇒Remove :

Remove method search given element if found then remove it, otherwise returns an Error.



General syntax for remove( ) :

list.remove(self, element)



Example :

list_1 = [1,2,3,4,5] 
list_1.remove(3) 
print(list_1)



Output :

[1, 2, 4, 5]




⇒ pop( ) :

Remove element of given index position and return removed elements, if index is not found returns Error.



General syntax for pop( ) :

list.pop(self, index)



Example :

list_1 = [1,2,3,4,5] 
list_1.pop() 
print(list_1)



Output :

[1, 2, 3, 4]




Another example :

list_1 = [1,2,3,4,5] 
list_1.pop(3) 
print(list_1)



Output :

[1, 2, 3, 5]




⇒ clear( ) : 

 It removes all the elements from the given list.



Example :

list_1 = [1,2,3,4,5] 
list_1.clear() 
print(list_1)



Output :

[ ]




More functions :

1. Reverse( ) : Reverse the order of the list.



General syntax for reverse( ) :

list.reverse(self)



Example :

list_1 = [1,2,3,4,5] 
list_1.reverse() 
print(list_1)



Output :

[5, 4, 3, 2, 1]


2. del : The del keyword removes the specified index.



Example :

list_1 = [1,2,3,4,5] 
del list_1[2] 
print(list_1)



Output :

[1, 2, 4, 5]



The del keyword removes the list completely.



Example :

list_1 = [1,2,3,4,5] 
del list_1



Output :



The above output will be blanked.



3. sort( ) : Sorts the list.



Example :

list_1 = [1,4,2,8,6,3] 
list_1.sort() 
print(list_1)



Output :

[1, 2, 3, 4, 6, 8]




Important Note : If you want to create a project like the ” CUSTOMER MANAGEMENT SYSTEM ” then you must remember the ” CRUD Operation “, this will make you easier to create the project.

I want to build a Customer Management System, so I will need CRUD Operation i.e. C ( create ) means  add customer, R ( read ) means search customer, U ( update ) means modify customer and D ( delete ) means delete customer.

If you make such a project, then you just have to remember the CRUD Operation.


>