Important List Operations in Python

Python programming language has important collections of data types such as List, Tuples, Set, and Dictionary, strings. These simple concepts are already sawed in the last post. suppose you can’t see the last post. Please know a little bit knowledge about that, what is data types, how many types are there, and how it was declared in python. I give a link below in this post.

In upcoming blogs, we will see what are operations are done using these data types. we have discussed some basic operations of these data types one by one. Now we can start with the List.

List

A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called an element. It can be of any type such as numbers, characters, strings, and even nested lists as well.

  • A list is an ordered, indexed, and sequence data type.
  • It is a mutable data type (It can be changeable). The elements can be modified or mutable which means the elements can be replaced, added, or removed.

Now, we know that elements in the list are mutable that means it can be modified. Some list of operations in python.

  • insert()
  • append()
  • extend()
  • pop()
  • remove()
  • del
  • index()
  • sort()
  • reverse()
  • count()

insert()

The insert() function inserts a value at the specified index in the list.

syntax of insert():

list.insert(index,value)

Example:

  • >>>list = [1,3,4,5,6]
  • >>>list.insert(1,2)
  • >>>print(list)
    • >>>[1,2,3,4,5,6]

append()

The append() function is used to adds a value in the end of the list.

Syntax of append():

list.append(value)

Example

  • >>>list = [1,2,3,4]
  • >>>list.append()
  • >>>print(list)
    • >>>[1,2,3,4,5]

extend()

extend() is used to adds the elements in a list to the end of another list. That is concatenated (+ or +=) both lists.

Syntax of the extend():

list1.extend(list2)

Example:

  • >>>list1 = [1,2,3,4]
  • >>>list2 = [5,6,7,8]
  • >>>list.extend(list2)
  • >>>print(list1)
    • >>>[1,2,3,4,5,6,7,8]

pop()

the pop() function removes the element at the specified index from the list. If there is no index is specified, then remove the last element from the list.

Syntax of pop():

  • list.pop(index) – removes the specified index from the list.
  • list.pop() – removes the last from the list.

Example 1:

  • >>>list = [1,2,3,4]
  • >>>list.pop(0)
  • >>>print(list)
    • >>>[2,3,4,5]

Example 2:

  • >>>list = [1,2,3,4]
  • >>>list.pop()
  • >>>print(list)
    • >>>[1,2,3]

remove()

remove() function is used to remove or delete the object from the list. If the object is not present in the list, then the interpreter threw a ValueError. If multiple elements are found then the first element will be deleted.

Syntax of the remove():

list.remove(object)

Example 1:

  • >>>list = [1,2,3,4]
  • >>>list.remove(4)
  • >>>print(list)
    • >>>[1,2,3]

Example 2:

  • >>>list = [1,2,3,4,1]
  • >>>list.remove(1)
  • >>>print(list)
    • >>>[2,3,4,1]

del

del method is used to del the whole list or del a specified index from the list.

Syntax of del:

  • del list – Deletes a whole list from the list.
  • del list[index] – Deletes a specific index from the list.

Example 1:

  • >>>list = [1,2,3,4]
  • >>>del list
  • >>>print(list)
    • >>> NameError: name ‘list’ is not defined

Example 2:

  • >>>list = [1,2,3,4]
  • >>>del list[0]
  • >>>print(list)
    • >>>[2,3,4]

index()

index() function is used to returns the index of the element in the list. Gives a ValueError if obj is not present in the list.

Syntax of index():

list.index(object)

Example:

  • >>>list = [1,2,3,4]
  • >>>print(list.index(2))
    • >>>1

sort()

sorts() function arranges the all elements in the list. (In Ascending order)

Syntax of sort():

list.sort()

Example:

  • >>>list = [4,3,2,1]
  • >>>list.sort()
  • >>>print(list)
    • >>>[1,2,3,4]

reverse()

reverse() is used to reverse the element in the list.

Syntax of reverse():

list.reverse()

Example:

  • >>>list = [1,2,3,4]
  • >>>list.reverse()
  • >>>print(list)
    • >>>[4,3,2,1]

count()

count() is used to counts the element, how many times is present in the list.

Syntax of count():

list.count()

Example:

  • >>>list = [1,2,3,1]
  • >>>print(list.count(1))
    • >>>2

My Previous Post:

Accessing index and value in list | Python: https://iterathon.tech//accessing-index-and-value-in-list-python/

Similar Posts

Leave a Reply

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