Lists In Python

Lists In Python

A list is kind of data type in python which are array like structure. In python, list holds homogeneous as well as heterogenous data, which means you can add string as well as integers in same list. You can intialise or create empty list with simple square brackets or using listkeyword, to create a list in python we will use this syntax for example:

Now we will get to know about different methods of list, that are as follows :

Append Method in List

Append method appends data to the end of list object. For example :

Clear Method in List

Clear method clears the contents of list. it removes all contents of list and makes it empty. For example:

Count Method in List

Count method counts the number of occurrence of object inside a list, it takes one parameter object which frequency we have to count. If it is not in list it returns 0, for example :

Extend Method in List

Extend method takes one argument as iterable and add it to end of list. An iterable can be any iterable object like list or tuples, for example :

Index Method in List

Index method returns the index of an object present inside list. we can also specify start and end value for searching in list. If the object is not inside a list it raises a value error, for example :

Insert Method in List

Insert method insert object to specific position, for example:

Pop Method in List

Pop method removes last element from list and returns it. It raises IndexError if list is empty or index out of range, for example:

Remove Method in List

Remove method takes object and remove first occurrence of it inside list and raises ValueError if value is not present inside list, for example:

Reverse Method in List

Reverse list object in place, means it will reverse object in memory, for example:

Sort Method in List

Sort list objects ascending or descending order, for example:you can read more at python lists.

Leave a Comment