In Python, you can use a list() function which creates a collection that can be manipulated for your analysis. This collection of data is called a list object.
While all methods are functions in Python, not all functions are methods. There is a key difference between functions and methods in Python. Functions take objects as inputs. Methods in contrast act on objects.
Python offers the following list functions:
sort(): Sorts the list in ascending order.
type(list): It returns the class type of an object.
append(): Adds a single element to a list.
extend(): Adds multiple elements to a list.
index(): Returns the first appearance of the specified value.
max(list): It returns an item from the list with max value.
min(list): It returns an item from the list with min value.
len(list): It gives the total length of the list.
list(seq): Converts a tuple into a list.
cmp(list1, list2): It compares elements of both lists list1 and list2.
filter(fun,list): filter the list using the Python function.
Python List Functions & Methods
Python sort list method
The sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.
Sort a list in ascending order
Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . (period) followed by the method name, i.e., sort including the parentheses.
prices =[238.11,237.81,238.91]
prices.sort()print(prices)# [237.81, 238.11, 238.91]Drivs av
Learn-by-Example-redigerare: tryck på Tab för att gå in. Tryck på Escape och sedan Tab eller Skift+Tab för att navigera ut.
These calls show that both fam and fam2 are, in fact, lists.
Python list append method
The append() method will add certain content you enter to the end of the elements you select.
Add a single element to a list
In this example, let's extend the string by adding "April" to the list with the method append(). Using append() will increase the length of the list by 1.
months =['January','February','March']
months.append('April')print(months)# ['January', 'February', 'March', 'April']Drivs av
Learn-by-Example-redigerare: tryck på Tab för att gå in. Tryck på Escape och sedan Tab eller Skift+Tab för att navigera ut.
The extend() method increases the length of the list by the number of elements that are provided to the method, so if you want to add multiple elements to the list, you can use this method.
Add multiple elements to a list
x =[1,2,3]
x.extend([4,5])print(x)# [1, 2, 3, 4, 5]Drivs av
Learn-by-Example-redigerare: tryck på Tab för att gå in. Tryck på Escape och sedan Tab eller Skift+Tab för att navigera ut.
The min() function will return the lowest value of the inputted values.
Find the smallest value in a list
In this example, you will find the month with the smallest consumer price index (CPI).
To identify the month with the smallest consumer price index, you first apply the min() function on prices to identify the min_price. Next, you can use the index method to find the index location of the min_price. Using this indexed location on months, you can identify the month with the smallest consumer price index.
months =['January','February','March']
prices =[238.11,237.81,238.91]# Identify min price
min_price =min(prices)# Identify min price index
min_index = prices.index(min_price)# Identify the month with min price
min_month = months[min_index]print(min_month)# FebruaryDrivs av
Learn-by-Example-redigerare: tryck på Tab för att gå in. Tryck på Escape och sedan Tab eller Skift+Tab för att navigera ut.
In the below example, you will be working with stock price data. Let's print out an empty list, convert a tuple into a list, and finally, print a list as a list.
# empty listprint(list())# tuple of stock prices
stocks =('238.11','237.81'
In Python, you can use a list() function which creates a collection that can be manipulated for your analysis. This collection of data is called a list object.
While all methods are functions in Python, not all functions are methods. There is a key difference between functions and methods in Python. Functions take objects as inputs. Methods in contrast act on objects.
Python offers the following list functions:
Python sort list method
The
sort()method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.Sort a list in ascending order
Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . (period) followed by the method name, i.e.,
sortincluding the parentheses.Python list type() function
For the
type()function, it returns the class type of an object.Check the data type of a list
Here we will see what type of both fam and fam2 are:
Let's see what the type of the object is:
Now, let's look at fam2.
Let's see what the type of the object is:
These calls show that both
famandfam2are, in fact, lists.Python list append method
The
append()method will add certain content you enter to the end of the elements you select.Add a single element to a list
In this example, let's extend the string by adding "April" to the list with the method
append(). Usingappend()will increase the length of the list by 1.Python list extend method
The
extend()method increases the length of the list by the number of elements that are provided to the method, so if you want to add multiple elements to the list, you can use this method.Add multiple elements to a list
Python list index method
The
index()method returns the first appearance of the specified value.Find an element's position
In the below example, let's look at the index of February in the list months.
This method helps identify that February is located at index
1. Now we can access the corresponding price of February using this index.Python list max function
The
max()function will return the highest value of the inputted values.Find the largest value in a list
In this example, we will look to use the
max()function to find the maximum price in the list namedprice.Python list min function
The
min()function will return the lowest value of the inputted values.Find the smallest value in a list
In this example, you will find the month with the smallest consumer price index (CPI).
To identify the month with the smallest consumer price index, you first apply the
min()function on prices to identify themin_price. Next, you can use the index method to find the index location of themin_price. Using this indexed location on months, you can identify the month with the smallest consumer price index.Python list len function
The
len()function shows the number of elements in a list. In the below example, we will look at stock price data again using integers.Count the elements in a list
Python list() function
The
list()function takes an iterable construct and turns it into a list. Here is the syntax:Convert a tuple to a list
In the below example, you will be working with stock price data. Let's print out an empty list, convert a tuple into a list, and finally, print a list as a list.