Python List Functions
A list function in Python is used to create a collection that can be influenced and is useful for data analysis and sorting. In Python programming, all methods are functions, but not all functions are methods. However, there is a distinct difference between them. In python functions, objects are taken as inputs. At the same time, methods act on the objects.
Python list functions provide different ways through which a user can sort data. Different list functions are used for various purposes. In this article, we will explore different list functions, their usage, and how to use them. We will understand the various Python list methods with the help of valuable examples.
Types of List Functions
Python programming language has a different type of python list methods that helps user manipulate data. Following are the various list functions:
Sort List Method
The sort () list is an in-built method in Python. You can sort data in ascending order using this method. You can also change it from descending to ascending by changing its criteria.
Syntax – sort()
For example,
age = [25, 19, 88, 43, 24, 52]
age.sort()
print(age)
Output:
[19,24,25,43,52,88]
Type List Function
This function is mainly used for debugging purposes. When used, it will show you the class type of the object.
Syntax – type()
For example,
marks = [“rahul”, 85, “manav”, 91, “shubam”, 79, “aditya”, 88 ]
marks
Output:
[‘rahul’, 85, ‘manav’, 91, ‘shubam’, 79, ‘aditya’, 88 ]
Input:
type(marks)
Output:
list
Append List Method
The append() list method adds content toward the end of the elements you select. This is a very useful and quick method of adding content to elements.
Syntax – append()
For example,
places = [‘Jaipur’, ‘Udaipur’, ‘Kota’]
places.append(‘Ajmer’)
print(places)
Output:
[‘Jaipur,’ ‘Udaipur,’ ‘Kota,’ ‘Ajmer’]
Extend List Method
The extend list method – extend() can add multiple elements/numbers provided to the method at the end of the current list. This method, unlike append, can add a multitude of elements.
Syntax – extend()
For example,
a = [1, 2, 3, 4]
a.extend([5,6])
a
Output:
[1, 2, 3, 4, 5, 6]
Index List Method
This method returns you to the first time when the given value appeared in the data. This is useful while analyzing and sorting data.
Syntax – index()
For example,
date = [‘12’, ‘13’, ‘14’, ‘15’]
day = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]
date.index(14)
Output:
2
Now we can use this data to find the corresponding data related to this value, In this case, the day.
Input:
print(day[3])
Output:
Tuesday
Max List Function
The max list function – max() will give you the maximum value among the input values. This is quite useful in data analysis.
Syntax – max()
For example,
#finding out the max-age
age = [25, 19, 88, 43, 24, 52]
age_max = max(age)
print(age_max)
Output:
88
Min List Function
The min list function – min() will give you the minimum value among the input values. This is, again, quite useful in data analysis and sorting.
Syntax – min()
For example,
#finding out the min age
age = [25, 19, 88, 43, 24, 52]
age_min = min(age)
print(age_min)
Output:
19
Len List Function
The len list function- len() is used to find the number of elements in a list. Let us look at the example below to understand this better –
Syntax – len()
For example,
January = [ 19, 27, 30 ]
February = [ 12, 21 ]
print(‘january length is ‘, len(january))
print(‘february length is ‘, len(february))
Output:
January length is 3
February length is 3
Python List Function
The Python List function – list() creates a list of objects. It is an in-built and widely used function. Let us look at the example below to understand it better-
Syntax – list()
For example,
day = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]
print(list(day))
Output:
[‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]
Compare or Cmp List Function
The Compare or Cmp list function – Cmp() compares two values against one another and returns the difference, whether it is positive, negative, or zero. It is a very useful tool in data sorting and analysis.
Syntax – Cmp ()
For example,
Rahul = [85]
Manav = [91]
print(cmp(rahul, manav)
print(cmp(rahul, rahul)
print(cmp(manav, rahul)
Output:
-6
0
6
This result showed us that Manav achieved more marks than Rahul and by what value. This function is especially useful while dealing with big numbers. If you want to get a deeper understanding of Python, you can read relevant blogs, attend seminars, and you can also take a Python course to enhance your career.
Filter List Function
The filter list function – filter() filters items through a function and returns the iterator after testing if the items were accepted or rejected. This helps us filter desirable elements from undesirable elements by using the function as a filter.
Syntax – filter ()
For example,
def filter_age(age):
if (age>30):
return True
Else:
return False
people_age = [25, 19, 88, 43, 24, 52]
filtered_age = filter(filter_age, people_age)
print(list(filtered_age))
Output:
[43,52,88]
as you may have noticed, this function is very useful especially while data sorting as it helps to separate unwanted elements from the entire dataset.
Let’s quickly summarize what we have learned so far –
Function | Description | Syntax |
Sort List Function | The sort () list is an in-built method in Python. You can sort data in ascending order using this method. | sort () |
Type List Function | When used, it will show you the class type of the object. | type() |
Append List Function | The append() list method adds content toward the end of the elements you select. | append() |
Extend List Function | The extend list method – can add multiple elements/numbers provided to the method at the end of the current list. | extend() |
Index List Function | This method returns you to the first time when the value appeared in the data. | index() |
Max List Function | The max list function will give you the maximum value among the input values. | max() |
Min List Function | The min list function will give you the minimum value among the input values. | min() |
Len List Function | The len list function is used to find the number of elements in a list. | Len() |
Python List function | The Python List function creates a list of objects | list() |
Compare or Cmp List Function | The Compare or Cmp list function compares two values against one another and returns the difference, whether it is positive, negative, or zero. | Cmp() |
Filter List Function | The filter list function filters items through a function and returns the iterator after testing if the items were accepted or rejected. | filter() |
Key Takeaway
Now let us summarize everything we learned about python list functions in the article:
- All functions are methods, but not all methods are functions
- Python list functions are essential for data sorting and analyzing.
- Python list functions enable you to sort data using different methods based on user needs.
- List functions can be used to add content to the elements in a quick and easy manner.
- List functions help users compare two values quickly, no matter how large they are.
- List functions help you to filter the desirable elements from the entire dataset.