Python Extras

15 Functions you should Know to Master Lists in Python

Introduction

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.

Python offers the subsequent list functions:

  • sort(): Sorts the list in ascending order.
  • type(list): It returns the class type of an object.
  • append(): Adds one element to a list.
  • extend(): Adds multiple elements to a list.
  • index(): Returns the first appearance of a particular value.
  • max(list): It returns an item from the list with a max value.
  • min(list): It returns an item from the list with a min value.
  • len(list): It gives the overall length of the list.
  • clear(): Removes all the elements from the list.
  • insert(): Adds a component at the required position.
  • count(): Returns the number of elements with the required value.
  • pop(): Removes the element at the required position.
  • remove(): Removes the primary item with the desired value.
  • reverse(): Reverses the order of the list.
  • copy():  Returns a duplicate of the list.

List Refresher

It is the primary, and certainly the foremost common container.

  • A list is defined as an ordered, mutable, and heterogeneous collection of objects.
  • To clarify: order implies that the gathering of objects follow a particular order
  • Mutable means the list can be mutated or changed, and heterogeneous implies that you’ll be able to mix and match any kind of object, or data type, within a list (int, float, or string).
  • Lists are contained within a collection of square brackets [ ].
Python Lists - javatpoint

Let’s see all the functions one by one with the help of an example,

sort() method

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

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.

prices = [589.36, 237.81, 230.87, 463.98, 453.42] 
prices.sort() 
print(prices)

Output:

[ 230.87, 237.81, 453.42, 463.98, 589.36]

type() function

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

Example

In this example, we will see the data type of the formed container.

fam = ["abs", 1.57, "egfrma", 1.768, "mom", 1.71, "dad"]
type(fam)

Output:

list

15 Functions you should Know to Master Lists in Python

download
Share
crown icon

Gunjan Goyal — June 12, 2021BeginnerProgrammingPython

This article was published as a part of the Data Science Blogathon

Introduction

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.

Learn Python Programming Online – Real Python

                                                 Image Source: Google Images

Python offers the subsequent list functions:

  • sort(): Sorts the list in ascending order.
  • type(list): It returns the class type of an object.
  • append(): Adds one element to a list.
  • extend(): Adds multiple elements to a list.
  • index(): Returns the first appearance of a particular value.
  • max(list): It returns an item from the list with a max value.
  • min(list): It returns an item from the list with a min value.
  • len(list): It gives the overall length of the list.
  • clear(): Removes all the elements from the list.
  • insert(): Adds a component at the required position.
  • count(): Returns the number of elements with the required value.
  • pop(): Removes the element at the required position.
  • remove(): Removes the primary item with the desired value.
  • reverse(): Reverses the order of the list.
  • copy():  Returns a duplicate of the list.

List Refresher

It is the primary, and certainly the foremost common container.

  • A list is defined as an ordered, mutable, and heterogeneous collection of objects.
  • To clarify: order implies that the gathering of objects follow a particular order
  • Mutable means the list can be mutated or changed, and heterogeneous implies that you’ll be able to mix and match any kind of object, or data type, within a list (int, float, or string).
  • Lists are contained within a collection of square brackets [ ].
Python Lists - javatpoint

                                                  Image Source: Google Images

Let’s see all the functions one by one with the help of an example,

sort() method

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

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.

prices = [589.36, 237.81, 230.87, 463.98, 453.42] 
prices.sort() 
print(prices)

Output:

[ 230.87, 237.81, 453.42, 463.98, 589.36]

type() function

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

Example

In this example, we will see the data type of the formed container.

fam = ["abs", 1.57, "egfrma", 1.768, "mom", 1.71, "dad"]
type(fam)

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.

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

Output:

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

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.

list = [1, 2, 3] 
list.extend([4, 5, 6]) 
list

Output:

[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.

months = ['January', 'February', 'March', 'April', 'May'] 
months.index('March')

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.

prices = [589.36, 237.81, 230.87, 463.98, 453.42] 
price_max = max(prices) 
print(price_max)

Output:

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.

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

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

Master Lists in Python

download
Share
crown icon

Gunjan Goyal — June 12, 2021BeginnerProgrammingPython

This article was published as a part of the Data Science Blogathon

Introduction

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.

Learn Python Programming Online – Real Python

                                                 Image Source: Google Images

Python offers the subsequent list functions:

  • sort(): Sorts the list in ascending order.
  • type(list): It returns the class type of an object.
  • append(): Adds one element to a list.
  • extend(): Adds multiple elements to a list.
  • index(): Returns the first appearance of a particular value.
  • max(list): It returns an item from the list with a max value.
  • min(list): It returns an item from the list with a min value.
  • len(list): It gives the overall length of the list.
  • clear(): Removes all the elements from the list.
  • insert(): Adds a component at the required position.
  • count(): Returns the number of elements with the required value.
  • pop(): Removes the element at the required position.
  • remove(): Removes the primary item with the desired value.
  • reverse(): Reverses the order of the list.
  • copy():  Returns a duplicate of the list.

List Refresher

It is the primary, and certainly the foremost common container.

  • A list is defined as an ordered, mutable, and heterogeneous collection of objects.
  • To clarify: order implies that the gathering of objects follow a particular order
  • Mutable means the list can be mutated or changed, and heterogeneous implies that you’ll be able to mix and match any kind of object, or data type, within a list (int, float, or string).
  • Lists are contained within a collection of square brackets [ ].
Python Lists - javatpoint

                                                  Image Source: Google Images

Let’s see all the functions one by one with the help of an example,

sort() method

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

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.

prices = [589.36, 237.81, 230.87, 463.98, 453.42] 
prices.sort() 
print(prices)

Output:

[ 230.87, 237.81, 453.42, 463.98, 589.36]

type() function

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

Example

In this example, we will see the data type of the formed container.

fam = ["abs", 1.57, "egfrma", 1.768, "mom", 1.71, "dad"]
type(fam)

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.

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

Output:

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

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.

list = [1, 2, 3] 
list.extend([4, 5, 6]) 
list

Output:

[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.

months = ['January', 'February', 'March', 'April', 'May'] 
months.index('March')

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.

prices = [589.36, 237.81, 230.87, 463.98, 453.42] 
price_max = max(prices) 
print(price_max)

Output:

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.

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

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.

months = ['January', 'February', 'March', 'April', 'May'] 
months.clear()

Output:

 [ ]

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button