What is a list in python, How to use list and its methods?

What is a List In Python?

The list is used to storing multiple or different types of data be it integer, character or floating-point in a single variable. The list is one of 4 collection data types.

List stores data in the indexed form and it is mutable, that is we can change the stored data using some methods. Also, the list allows duplicate members.

How to use List?

  1. We declare a list using square brackets [ ] and inside those square brackets, we gave our data.

Code:list in python

 

Output:

  1. We can use a list using a constructor called list(). In constructor list() we will pass our data in round brackets.

 

Code:

Output:

list-in-python

Note: – Our data is passed into square brackets and the to list() constructor.

What are list methods or what is methods in list?

List method or methods in list is pre-written codes which we can use to deliver some functionality.

Example we want to add some element to the existing list we will use append() method using dot ( . ) operator. Likewise append method there are different built-in methods as well.

Methods Description
append(x) Adds an element to list.
clear() Removes all the element from the list
copy() Return a copy of whole list
count(x) Returns the occurrence of particular element in the list
extend(x) Adds a list or iterable object at the end of the existing list
index(x) Returns the index of a particular element
insert (x, x) Adds an element or iterable object to the specified location
pop(x(optional)) Removes and delete element from the end or specified position
remove(x) Removes and delete by taking element directly
reverse() Reverse the order of list
sort() Sort the list in ascending order by default.

 

Note:- The “X” inside method represents that method takes an argument or not, it has nothing to do actual while coding.

Summary

  • List is one of 4 collection datatypes
  • List is indexed
  • List is mutable
  • Allows duplicate members

Leave a Comment

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