15 Functions You Should Know To Master Lists in Python
15 Functions You Should Know To Master Lists in Python
In Python, you’ll be able to use a list function that creates a group that
will be manipulated for your analysis. This collection of data is named a
list object.
While all methods are functions in Python, not all functions are methods.
There’s a key difference between functions and methods in Python.
Functions take objects as inputs while Methods in contrast act on
objects.
Example
Let’s say you would like to sort the elements of the product’s prices in
ascending order. You’d type prices followed by a . (period) followed by
the method name, i.e., sort including the parentheses.
Output:
Example
In this example, we will see the data type of the formed container.
Output:
list
append() method
The append() method will add some elements you enter to the end of the
elements you specified.
Example
In this example, let’s increase the length of the string by adding the
element “April” to the list. Therefore, the append() function will
increase the length of the list by 1.
Output:
extend() method
The extend() method increases the length of the list by the number of
elements that are provided to the strategy, so if you’d prefer to add
multiple elements to the list, you will be able to use this method.
Example
In this example, we extend our initial list having three objects to a list
having six objects.
[1, 2, 3, 4, 5, 6]
index() method
The index() method returns the primary appearance of the required
value.
Example
In the below example, let’s examine the index of February within the list
of months.
Output:
2
max() function
The max() function will return the highest value from the inputted
values.
Example
In this example, we’ll look to use the max() function to hunt out the
foremost price within the list named price.
589.36
min() function
The min() function will return the rock bottom value from the inputted
values.
Example
In this example, you will find the month with the tiniest consumer
indicator (CPI).
To identify the month with the tiniest consumer index, you initially
apply the min() function on prices to identify the min_price. Next, you’ll
use the index method to look out the index location of the min_price.
Using this indexed location on months, you’ll identify the month
with the smallest consumer indicator.
Output:
February
len() function
The len() function returns the number of elements in a specified list.
Example
In the below example, we are going to take a look at the length of the
2 lists using this function.
list_1 = [50.29]
list_2 = [76.14, 89.64, 167.28]
print('list_1 length is ', len(list_1))
print('list_2 length is ', len(list_2))
Output:
list_1 length is 1
list_2 length is 3
clear() function
The clear() method removes all the elements from a specified list and
converts them to an empty list.
Example
In this example, we’ll remove all the elements from the month’s list and
make the list empty.
Output:
[ ]
insert() function
The insert() method inserts the required value at the desired position.
Example
In this example, we’ll Insert the fruit “pineapple” at the third position of
the fruit list.
Output:
Example
In this example, we are going to return the number of times the
fruit “cherry” appears within the list of fruits.
Output:
3
pop() function
The pop() method removes the element at the required position.
Example
In this example, we are going to remove the element that’s on the third
location of the fruit list.
Output:
Example
In this example, we will Remove the “banana” element from the list of
fruits.
Output:
Example
In this example, we will be reverse the order of the fruit list, so that the
first element in the initial list becomes last and vice-versa in the new list.
Output:
Example
In this example, we want to create a list having the same elements as the
list of fruits.
Output:
# number in a list
# list of numbers
list1.sort()
# number in a list
# list of numbers
Output:
Largest element is: 99
Method 3 : Find max list element on inputs provided by user
# number in a list
list1 = []
list1.append(ele)
Output:
Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 1
Enter elements: 99
Largest element is: 99
Method 4 : Without using built in functions in python:
# number in a list
def myMax(list1):
max = list1[0]
for x in list1:
max = x
return max
# Driver code
Output:
Largest element is: 99