List functions of Python | List Methods of Python

You must first complete Display output in Python 3 | print() function in Python 3 before viewing this Lesson



List functions of Python | List Methods of Python

Python provides many predefined functions and methods for manipulation of list.

1. index()

This method is used to find first index position of a value in a list. It returns-1 if value is not found in the list.

Syntax:

List.index (<Val>)

List is user defined list.

<Val> refers to the value whose index we want to find in List.

Example 1:

>>> L1=[13,18,11,16,18,14]
>>> print(L1.index(18))
1

#Index of first occurance of 18 is shown i.e. 1.

Example 2:

>>> print(L1.index(10))
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
L1.index(10)
ValueError: 10 is not in list

# Above example shows error as 10 is not in the list

2. append()

This method is used to add a new element at the end of list.
Syntax:
List.append(<Val>)
List is user defined list.
<Val> refers to the value we want to add to List.
Example 1:

>>> L1=[13,18,11,16,18,14]
>>> L1.append(100)
>>> print(L1)
[13, 18, 11, 16, 18, 14, 100]

Example 2:

>>> L1=[13,18,11,16,18,14]
>>>L1.append(12,14)
Traceback (most recent call last):
File’<pyshell#4>”,line1, in <module>
T1.append(12,14)
Type Error: append ()takes exactly one argment (2 given)

#Error occurs as we can’t append multiple values.

Example 3:

>>> L1=[13,18,11,16,18,14]
>>> L1. Append([12,14])
>>> print(L1)
[1,3,5,10,[12,14]]

#List [12,14] is appended as an individual element to list L1




3. extend()

The extend() method is used to append elements to another list to existing list..
Example 1:

>>>L1=[‘a’,’b’,’c’,]
>>>L2=[‘d’,’e’]
>>>L1.extend(t2)
>>> print(L1)
[‘a’,’b’,’c’,’d’,’e’]

#In the above example, elements of list L2 are added to list L1. But list L2 remains unchanged.

Example 2:

>>>L1=[‘a’,’b’,’c’,]
>>>L1. Extend(10)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
L1.extend(10)
TypeError: 'int' object is not iterable#Error occurs as we can’t extend a list by a single value.

4. insert()

The insert () method is used to insert an element at any position in a list.
Syntax:
List .insert (<position>,<item>)
<position> is the index of the element before which the <item> should be added.
<item> is the value to be inserted into list.
Example 1:

>>> L1=[13,18,11,16,18,14]
>>> L1.insert(1,100)  #insert 100 at index 1 i.e. second element.
>>> print(L1)
[13,18,100,11,16,18,14]

Example 2

>>> L1=[13,18,11,16,18,14]
>>> L1.insert(20,100)

#100 is inserted as last element as index 20 exceeds list index range.

>>> print(L1)
[13, 18, 11, 16, 18, 14, 100]

Example 3:

>>> L1=[13,18,11,16,18,14]
>>> L1.insert(-2,100)

#100 is inserted as last element as index -2.

>>> print(L1)
[13, 18, 11, 16, 100, 18, 14]

Example 4:

>>> L1=[13,18,11,16,18,14]
>>> L1.insert(0,100)

#100 is inserted as first element at index 0.

>>> print(L1)
[100, 18, 11, 16, 18, 14]

Example 5:

>>> L1=[13,18,11,16,18,14]
>>> L1.insert(len(L1),100)

#100 is inserted at last of list.

>>> print(L1)
[13,18, 11, 16, 18, 14,100]

Example 6:

>>> L1=[13,18,11,16,18,14]
>>> L1.insert(-20,100)

#100 is inserted as first element of list as -20 is out of index range.

>>> print(L1)
[100,13,18, 11, 16, 18, 14]



5. pop()

pop() method is used to remove and return an element from the given index in the list. If no index is specified, pop() removes and returns the last item in the list.
Syntax:
List .pop (<index>)
<index> specifies the position from where element is to be deleted. It is optional argument.
Example 1

>>> L1=[13,18,11,16,18,14]
>>> val=L1.pop()  #Deletes last element of list and stores in val.
>>> print(val)
14
>>> print(L1) #List will not contain 14
[13, 18, 11, 16, 18]

Example 2

>>> L1=[13,18,11,16,10,14]
>>> val=L1.pop(1)  #element at index 1 i.e. 18 is deleted and stored in val
>>> print(val)
18
>>> print(L1) 
[13, 11, 16, 10, 14]

Example 3

>>> L2=[]                         #empty list
>>>L2.pop()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
L2.pop()
IndexError: pop from empty list. 

6. remove()

The remove () method removes the first occurrence of an item from the list.
Syntax:
List .remove(<value>)
<value>  refers to any element of list that is to be deleted.
Example 1

>>> L1=[13,18,11,16,18,14]
>>> L1.remove(11)
>>> print(L1) #List will not contain 11
[13, 18, 16, 18, 14]

Example 2

>>> L2=['a','e','i','o','u']
>>> L1.remove('k')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
L1.remove('k')
ValueError: list.remove(x): x not in list

7. clear()

This method is used to remove all the items from a list and make the list empty. It doesn’t delete the list permanently.
Syntax:
List.clear()
Here List refers to existing which we want to clear.
Example:

>>> L1=[13,18,11,16,18,14]
>>> L1.clear()
>>> print(L1)
[ ]




8. count()

This function is used to count and return number of times a value exists in a list. If the given value is not in the list, it returns zero.
Syntax:
List.count(<value>)
<value> refers to the value whose count we want.
Example 1:

>>> L1=[13,18,11,16,18,14]
>>> L1.count(18)  #18 appears twice in list L1.
2

Example 2:

>>> L1=[13,18,11,16,18,14]
>>> L1.count(30)   #30 doesn’t exist in the list L1.
0

9. reverse()

It is used to reverse the items of the list.
Syntax:
List.reverse()
List refer to the name of existing list that we want to reverse.
Example:

>>> L1=[13,18,11,16,18,14]
>>> L1.reverse()
>>> print(L1)
[14, 18, 16, 11, 18, 13]

10. sort()

This function arranges the items of the list in increasing order. We can arrange elements in decreasing order by using function as sort (reverse=True).
Syntax:
List .sort()
List refers to existing list that we want to sort.
Example 1:

>>> L1=[13,18,11,16,18,14]
>>> L1.sort()
>>> print(L1)
[11, 13, 14, 16, 18, 18]

Example 2:

>>> L1=[13,18,11,16,18,14]
>>> L1.sort(reverse=True)
>>> print(L1)
[18, 18, 16, 14, 13, 11]



Spread the love
Lesson tags: append() method of python, clear() method of python, count() method of python, extend() method of python, index() method of python, insert() method of python, List functions of Python, List functions of Python 3, List methods of Python 3, pop() method of python, remove() method of python, reverse() method of python, sort() function of python
Back to: Python Programming Tutorial
Spread the love