codingstreets
Search
Close this search box.
python-list-methods

Introduction To Python List Methods With Practical Questions

In This Article You Will Learn About Python List Methods

Python List Methods – Before moving ahead, let’s take a look of Introduction to Python List

List Methods – Python has a set of built-in methods that can be used on lists.

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

Syntax - list.count(value)

Parameter values - It is required argument and takes letter or word which is to be counted.

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

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

print(z)
python-list-methods
As it is shown clearly that it returned number of times letter ‘a’ is presented.

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

HELLO
12345
Index number
Syntax - list.index(value)

Parameter Values - It is required argument and takes element which is to be indexed

Example 24- Use of index() method to get index number of particular letter.

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

reverse() method – It reverse the order of list. For example – first element will shift to end and last element will come first.

Syntax - list.reverse()

Parameter Values - No parameter values.

Example 25- It reversed order of list.

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

x.reverse()
print("after reversing:" , x)
python-list-methods
As it is shown clearly that it retuned the reverse order of list elements

sort() method – It arranged list in ascending order by default.

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

Parameter Values - It is optional argument. By default, it will sort list in ascending order, otherwise reverse =True will sort in descending order.

Example 26-  Use of sort() method to sort the list in Ascending order.

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

x.sort()
print("after sort", x)
python-list-methods
As it is shown clearly that it returned number of elements of list in Ascending order.

Example 27-  Use of sort() method to sort the list in descending order.

x = [2 , 5 , 1 , 3]

print("before sort:" , x)
x.sort(reverse=True)

print("after sort" , x)
python-list-methods
As it is shown clearly that it returned number of elements of list in Descending order.

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

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on