Beginner Guide To Python List Method With Practical Questions

In this article you will learn about python list methods

Python List Method – Before moving ahead, let’s know a little bit about Python List

Here’s the list of Python List() Method –

Method NameDescription
append()It adds an element at the end of the list
clear()It removes the all elements of the list
copy()It makes a copy of original list
count()It returns the number of elements of specified value
extend()It adds the whole list at the end of list
index()It returns the index number of specified element
insert()It inserts an element at a particular place
pop()It removes the last element of list by default
remove()It removes the specified element from the list
reverse()It reverses the elements of the list
sort()It sorts the list. In other words, sets elements by alphabetically  

Let’s start with examples –

append() – It adds an element at the end of the list

Syntax – list.append(element)

Parameter Values -         

Element – It is required. It includes element of any type like string, number, object etc.

Example 1- Add element at the end position.

x = ['Welcome' , 'in' , 'Python']
print('before adding:' , x)

x.append('language')
print('After adding:' , x) 
python-list-method
As it is shown clearly that it returned updated list.

Example – 2 Add a whole list as an element.

x = ['Welcome' , 'in' , 'Python']
print('before adding:' , x)

x.append['language' , to learn it])
print('After adding:' , x) 
python-list-method
As it is shown clearly that it returned updated list.

clear() method –  It removes the all elements of the list.

Syntax – list.clear()

Parameter Values – No Parameter Values

Example – removes all elements of list

x = ['Welcome' , 'in' , 'Python']
print('before removing elements:' , x)

x.clear()
print('After removing elements:' , x)
python-list-method
As it is shown clearly that it returned removed all elements from list.

copy() –  It makes a copy of original list

Syntax – list.copy()

Parameter Values – No Parameter Values

Example – Use of copy() method to copy a list.

x = ['Python', 'Java', 'HTML']
z = x.copy()

print(x)
python-list-method
As it is shown clearly that it returned copy of original list

count() – It returns the number of elements of specified value.

Syntax – list.copy()

Parameter Values –

Value – It is required value and defined as the value to search for.  It can be any type of value like (string, number, list, tuple, etc.). 

Example 1- Use of count() method to count specified value (alphabet).

x = 'language'
z = x.count('a')

print(z)
python-list-method
As it shown clearly that it returned the maximum number of times letter ‘a’ is appeared.

Example 2- Use of count() method to count specified value (number).

x = [1 , 2 , 3 , 1 , 5 , 1 , 6 , 1]
z  = x.count(1)

print(z)
python-list-method
As it shown clearly that it returned the maximum number of times number 1 is appeared.

extend() – It adds the whole list at the end of list

Syntax – list.extend(elements)

Parameter Values –

Elements – It is Required. The element (list, tuple, set etc.) or list of elements to be added.

Example 1 – Adding an element.

x = ['Python' , 'Java' , 'HTML']
x.extend(['C++'])

print(x)
python-list-method
As it is shown clearly that it added element at the last position of the list.

Example 2 – Adding a list of elements.

x = ['Python' , 'Java' , 'HTML']
x.extend(['C++', 'Java', 'C#']) 

print(x)
python-list-method
As it is shown clearly that it added whole list at the last position of the list.

index() – It returns the index number of first occurrence specified element.  

Syntax – list.index(element, value, value)

Parameter Values –

element – It is Required. The element to be searched for index number.

 Example 1- Using index() method to get index number of particular element.

x = ['a', 'b', 'c', 'd']
print(x.index('c'))
python-list-method
As it is shown clearly that it returned index number of specified letter.

Example 2 – Using index() method to get index number of first occurrence specified element between range.

x = ['a' , 'c' , 'g' , 'd' , 'c' , 't']
print(x.index('c' , 0 , 5))
python-list-method
As it is shown clearly that it returned index number of specified letter between the range.

Note: Index number starts from 0.

insert() – It inserts an element at a particular place.

Syntax – list.insert(value, element)

Value – It is Required. A value where element is to be inserted.

Element – It is Required. It is element that to be inserted.

Example 1 – Inserting ‘JavaScript’ at index number 2.

x = ['Python' , 'Java' , 'HTML']
x.insert(2 , 'JavaScript')

print(x)
python-list-method
As it is shown clearly that it inserted string at specified index number

pop() – It removes the last element of list by default.

Syntax – list.pop(value)

Parameter Value –

Value – It is optional. A number specifying which element has to remove from list. By default, it removes last element of list that’s index number is -1.

Example 1- Using pop() method to remove last element.

x = ['Python' , 'Java' , 'HTML']
print("before removing:" , x)

x.pop()
print("After removing:" , x)
python-list-method
As it is shown clearly that it removed last element of the list by default.

Example 2- Using pop() method to remove specified element.

x = ['Python' , 'Java' , 'HTML']
print("before removing:" , x)

x.pop(1)
print("After removing:" , x)
python-list-method
As it is shown clearly that it removed element from the list at the specified index number.

remove() – It removes the specified element from the list.

Syntax – list.remove(element)

Parameter Value -

Element – It is Required. It can be any type of element like string, number, list etc. that has to remove. 

Example 1- Using remove() method to remove any element.

x = ['Python' , 'Java' , 'HTML']
x.remove('Java') 

print(x)
python-list-method
As it is shown clearly that it removed specified element from the list.

Example 2- showing an error while removing element.

x = ['Python' , 'Java' , 'HTML']
x.remove('c')

print(x)
python-list-method
As it is shown clearly that it returned an error as element is not present in the list

reverse() – It reverses the elements of the list.

Syntax – list.reverse()

Parameter Values – No Values

Example – It reversed order of element of list.

x = ['a' , 'b' , 'c' , 'd']
print('before reversing:' , x)

x.reverse()
print("after reversing:" , x)
python-list-method
As it is shown clearly that order of element is reversed.

sort() – It sorts the list. In other words, sets elements by alphabetically.

Syntax - list.sort(reverse = True|False, key)

Parameter Values –

reverse - It is an optional argument. By default, it will sort the list in ascending order, 

otherwise reverse =True will sort in descending order.

Key – It is Optional. It is a function describes sorting criteria. 

Example 1- Using sort() method to sort the list.

x = [2 , 5 , 1 , 3]
print("before sort:" , x)

x.sort()
print("after sort" , x)
python-list-method
As it is shown clearly that it returned a sort list in ascending order.

Example 2- Using sort() method to sort the list by counting the length of element.

def myfunc(name):
      return len(name)
name = ['Python' , 'Java' , 'C++']
name.sort(key=myfunc)

print(name)
python-list-method
As it is shown clearly that list is sort according to elements size.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets