Python list manipulation functions

In Python , list is a datatype which consists of ordered collection of data . Lists in python can store any type of data such as integers, floating numbers, strings, characters, and even other lists. Lists are mutable ,such that elements in the list can be changed after its creation.

Set of available functions that manipulate the given list.

Python provides several built-in functions that helps manipulating list elements. Some of them are mentioned below:

  • append()
  • insert()
  • pop()
  • remove()
  • sort()
  • reverse()

We will read about the above functions in detail here:

Append() function.

This function in list will add the given item at the end of the list given.

list = [3, 5, 6]
list.append(4)
print(list)

Insert () function

This function in list will insert the data at the given index.

list = [4, 7, 8]
list.insert(1, 5)
print(list)

Pop() function

This function removes the item at the specified index (or the last item if index is not provided) and returns it.

 

list = [1, 9, 0]
pop_elem = list.pop(1)
print(list)     
print(pop_elem)

Remove () function

This function removes the first occurred element.

list = [1, 5, 5, 7]
list.remove(5)
print(list)

Sort () function

This function sorts the given elements in the ascending order.

list = [3, 9, 2]
list.sort()
print(list)

Reverse () function

This function is used to print the given list in reverse order.

list = [5, 9, 3]
list.reverse()
print(list)

These are some of the list manipulating functions that are in-built in the List datatype.

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top