List Functions and methods in Python | List manipulation in python | Class XII Computer science

List Functions and methods in Python 

Hi guys in this blog we are going to discuss about List methods in Python List |  Functions and methods in Python 

List Functions

Python also offers many built-in functions and methods for list manipulation. These can be applied to list as per following syntax :

<listObject>.<method name>()


1.The index method

This function returns the index of first matched item from the list.

List.index (<item>) For example, for a list L1 = [13, 18, 11, 16, 18, 14),

>>> L1.index (18)

returns the index of first value 18, even if there is another value 18 at index 4.


2. The append method

The append() method adds an item to the end of the list. It works as per following syntax:

List.append(<item>

- Takes exactly one element and returns no value

For example, to add a new item "yellow" to a list containing

>>> colours = ['red', 'green', 'blue']

>>> colours.append('yellow)

>>> colours

['red', 'green', 'blue', 'yellow]


3. The extend method

The extend() method is also used for adding multiple elements (given in the form of a list) to a list.

The extend() function works as per following

List.extend<list>

That is extend() takes a list as an argument and appends all of the elements of the argument list to the list object on which extend() is applied. Consider the following example:

>>> t1 = ['a', 'b', 'c']

>>>t2 = ['d', 'e']

>>> t1.extend(t2)

['a', 'b', 'e', 'd', 'e']

>>>t2


4. The insert method

The insert() function inserts an item at a given position. It is used as per following syntax:

List.insert( <pos>, <item>)

- Takes two arguments and returns no value.

The first argument <pos> is the index of the element before which the second argument <item> is to be added. Consider the following example:

>>> t1 = ['a', 'e', 'u']

>>> ti.insert(2, '1')

['a', 'e', 'T', 'u]


5. The pop method

The pop() is used to remove the item from the list. It is used as per following syntax:

List.pop(<index>)


The remaining methods in List are

6.remove()

7.clear()

8.count()

9.reverse()

10.sort()

Will Discuss in next post about these methods in list

If you any doubts, Please let me know

Previous Post Next Post

Contact Form