Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Python List Functions & Python List Methods

Last Updated on October 30, 2023 by Prepbytes

The list function in Python allows you to generate a manipulable collection for your study. A list object is the name given to this group of data.

What is Python List Function?

List functions are global functions that can be used with any list. They take a list as an argument and return a value. Here are a few examples of list functions:

Python offers the following list functions:

• len(): Returns the number of elements in the list.
• sorted(): Returns a new sorted list of the elements in the original list.
• min(): Returns the smallest element in the list.
• max(): Returns the largest element in the list.
• sum(): Returns the sum of all elements in the list.

What is Python List Method?

List methods, on the other hand, are built-in functions that are specific to the list data type.

Python offers the following List Methods:

• append(): Adds an element to the end of the list.
• insert(): Inserts an element at a specified position in the list.
• remove(): Removes the first occurrence of an element from the list.
• pop(): Removes and returns the element at a specified index.
• sort(): Sorts the elements in the list in ascending order.

Difference between Python List Functions & Python List Methods

Here we have basicdifference Python list functions & Python list Methods

Python List Method Python List Function
List methods, on the other hand, are built-in functions that are specific to the list data type. These methods can only be used with lists and modify the list in place. List functions are global functions that can be used with any list. These functions take a list as an argument and return a value.
Some examples of list methods are append(), insert(), and remove(). Some examples of list functions are len(), sorted(), and min().

Python List Functions & Methods

Below are the Python list functions and python list methods:

Python Sort List Method

The list is sorted in ascending order by default using the built-in Python function sort(). By defining the sorting criteria, you may change the order from ascending to descending, though.

Example of python sort list method
Consider sorting the element’s prices in ascending order. You would put prices, a. (period), the name of the technique, i.e., sort with the parenthesis, and then prices.

``````prices = [238.11, 237.81, 238.91]
prices.sort()
print(prices)``````

Output:

``[237.81, 238.11, 238.91]``

Python List type() Function

The class type of an object is returned by the type() method.

Example of list type() fucntion
Here we will see what type of both fam and fam2 are:

``````fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]
fam

['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]``````

Let’s see what the type of the object is:

``````type(fam)

list``````

Now, let’s look at fam2.

``````fam2 = [["liz", 1.73],
["emma", 1.68],
["mom", 1.71],
fam2

[['liz', 1.73], ['emma', 1.68], ['mom', 1.71], ['dad', 1.89]]``````

Let’s see what the type of the object is:

``````type(fam2)

list``````

These calls demonstrate the fact that fam and fam2 are both lists.

Python List Append Method

Using the append() function, you may add specific text at the end of the components you’ve chosen.

Example of list append method
Let’s expand the string in this example by adding "April" to the list using the function append (). The list will grow by 1 when append() is used.

``````months = ['January', 'February', 'March']
months.append('April')
print(months)``````

Output:

``['January', 'February', 'March', 'April']``

Python List Extend Method

If you wish to add extra elements to the list, you may use the extend() method, which lengthens the list by the number of elements that are supplied to the method.

Example of list extend method

``````x = [1, 2, 3]
x.extend([4, 5])
x``````

Output:

``[1, 2, 3, 4, 5]``

Python List Index Method

The supplied value’s first occurrence is returned by the index() function.

Example of list index method
In the below example, let’s look at the index of February in the list of months.

``````months = ['January', 'February', 'March']
prices = [238.11, 237.81, 238.91]

months.index('February')

1``````

This technique makes it easier to see that February is at index 1. Using this index, we can now get the comparable price for February.

``````print(prices[1])

237.81``````

Python List Max Function

The maximum value of the given values will be returned by the max() method.

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

``````# Find the maximum price in the list price
prices = [159.54, 37.13, 71.17]
price_max = max(prices)
print(price_max)``````

Output:

``159.54``

Python List Min Function

The lowest value from the given numbers will be returned by the min() method.

Example of list min function
In this example, you will find the month with the smallest consumer price index (CPI).

Applying the min() method on prices to determine the min price is the first step in determining the month with the least consumer price index. The index method may then be used to locate the min price’s index position. The month with the lowest consumer price index may be found using this index of months.

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

Output:

``February``

Python List Len Function

The len() method displays a list’s element count. We will once more examine stock price data using integers in the example below.

Example of python list len function

``````stock_price_1 = [50.23]
stock_price_2 = [75.14, 85.64, 11.28]

print('stock_price_1 length is ', len(stock_price_1))
print('stock_price_2 length is ', len(stock_price_2))``````

Output:

``````stock_price_1 length is 1
stock_price_2 length is 3``````

Python list() Function

The list() method creates a list from an iterable construct.

Syntax of Python list() function

``list([iterable])``

Example of python list() function
You will be working with stock price data in the example below. Let’s publish a tuple as a list, then a list as a list, and then an empty list.

``````# empty list
print(list())

# tuple of stock prices
stocks = ('238.11', '237.81', '238.91')
print(list(stocks))

# list of stock prices
stocks_1 = ['238.11', '237.81', '238.91']
print(list(stocks_1))``````

Output:

``````[]
['238.11', '237.81', '238.91']
['238.11', '237.81', '238.91']``````

Python cmp() Function

When using the cmp() function, two numbers are taken and compared to one another. Depending on the input, it will then return a negative, zero, or positive result.

Example of python cmp function
We will compare the integer numbers in the following example to determine which of two stock prices is larger:

``````stock_price_1 = [50.23]
stock_price_2 = [75.14]

print(cmp(stock_price_1, stock_price_2))
print(cmp(stock_price_1, stock_price_1))
print(cmp(stock_price_2, stock_price_1))``````

Output:

``````-1
0
1``````

The results indicate that the size of the stock price 2 list is greater than that of the stock price 1 list. The cmp() method works with any kind of list, including strings. Keep in mind that if one list is longer than the other, it will automatically be regarded as greater.

Python Filter List Function

With the filter() function, a function, a list, and an iterator of filtered elements are sent in. To obtain a filtered list, you may either write a Python filter function or use a lambda function.

Note: To obtain a filter list, you may also use loops, list comprehension, and string pattern matching.

Example of python filter list function
We’ll start by developing a filter function that outputs boolean results. It will be composed of basic logical operations. In this instance, we will filter out items with prices more than 350. The method will then be applied on the item price list using filter(), returning an iterator of the filtered elements.
Either use a for loop to extract value or turn an iterator into lists to have access to the filtered items.

``````def filter_price(price):
if (price > 350):
return True
else:
return False

item_price = [230, 400, 450, 350, 370]

# applying filter function
filtered_price = filter(filter_price, item_price)

print(list(filtered_price))``````

Output:

``[400, 450, 370]``

Using a lambda function, you may also reduce your code to a single line and obtain filtered elements.

``````# lambda function with filter()
filtered_price = filter(lambda a: a > 350, item_price)
print(list(filtered_price))``````

Output:

``[400, 450, 370]``

Conclusion
Till now, we have discussed Python lists functions and python lists methods with examples. So we hope you will understand the topic clearly. Now, let us look at some FAQs related to the topic.

FAQs related to Python List function and Methods

1. Is list() a method in python?
A list object is created using the list() method. An ordered modifiable collection is referred to as a list object. The chapter on Python Lists has further information about lists.

2. What are the 4 types of functions in python?
The following are the different types of Python Functions:

• Python Built-in Functions.
• Python Recursion Functions.
• Python Lambda Functions.
• Python User-defined Functions.

3. What are 3 types of lists in python?
One of Python’s four built-in data types four built-in data types in Python for storing data collections is the list; the other three are the tuple, set, and dictionary, each of which has a unique purpose.

4. How many methods does list have?
Four options are available for positional (indexed) access to list elements using the List interface. Lists are zero-based, just like Java arrays. Note that in certain implementations, the time it takes for these operations to complete may be proportional to the index value (the LinkedList class, for example).

*5. What is the use of () operator in list?*
Repeat Operator on List Items (
). The * operator is a feature of Python List that enables you to create a new list with the specified number of times repeated elements.

6. Is list mutable or Immutable?
A list data type is mutable. After a list has been established, its components can be changed. Values can be replaced separately.