  Introduction To Python List Methods With Practical Questions

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)```

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

```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'))```

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)```

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)``` 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)``` 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

Share on