Python List Functions

In Python, we can use a list function which creates a collection that can be manipulated for our 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 & 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 & list2.

sort() Method

The sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, we can modify the order from ascending to descending by specifying the sorting criteria.

Example

Let’s say we want to sort the element in prices in ascending order. We would type prices followed by a . (period) followed by the method name, i.e., sort including the parentheses.

type() Function

For the type() function, it returns the class type of an object.

Example

Here we will see what type of both fam & 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 fam & fam2 are in fact lists.

append() Method

The append() method will add certain content we enter to the end of the elements we select.

Example

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.

When we run the above code, it produces the following result:

extend() Method

The extend() method increases the length of the list by the number of elements that are provided to the method, so if we want to add multiple elements to the list, we can use this method.

Example

When we run the above code, it produces the following result:

index() Method

The index() method returns the first appearance of the specified value.

Example

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.

max() Function

The max() function will return the highest value of the inputted values.

Example

In this example, we will look to use the max() function to find the maximum price in the list named price.

When we run the above code, it produces the following result:

min() Function

The min() function will return the lowest value of the inputted values.

Example

In this example, we will find the month with the smallest consumer price index (CPI).

To identify the month with the smallest consumer price index, we first apply the min() function on prices to identify the min_price. Next, we can use the index method to find the index location of the min_price. Using this indexed location on months, we can identify the month with the smallest consumer price index.

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.

Example

When we run the above code, it produces the following result:

list() Function

The list() function takes an iterable construct & turns it into a list.

Syntax

Example

In the below example, we will be working with stock price data. Let’s print out an empty list, convert a tuple into a list, & finally, print a list as a list.

When we run the above code, it produces the following result:

cmp() Function

For the cmp() function, it takes two values & compares them against one another. It will then return a negative, zero, or positive value based on what was inputted.

Example

In the example below, we have two stock prices, & we will compare the integer values to see which one is larger:

When we run the above code, it produces the following result:

The results show that the stock_price_2 list is larger than the stock_price_1 list. We can use the cmp() function on any type of list, such as strings. Note that by default, if one list is longer than the other, it will be considered to be larger.

RELATED LINKS

https://bjc.edc.org/Jan2017/bjc-r/cur/programming/old-labs/python/list_functions.html?topic=nyc_bjc%2FNA-python.topic

Data Scientist & Machine Learning Engineer