leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Functions related to Python Lists

Estimated reading: 4 minutes 28 views
Functions related to Python Lists

For lists, in addition to setting and modifying the elements, there are some other methods that are also useful, let’s take a look.

Python get the length of a list

Sometimes it is helpful to know how many elements are in the list. For example, if we keep adding fruit to the fruits, we might forget how many kinds of fruits there are. Use the len() function to quickly get the length of a list. In the example below, the fruits list contains 5 elements, so its length is 5:

>>> fruits=["apple","cherry","banana","orange","grape"]
>>> len(fruits)
5


There are 5 elements in the fruits list, and we know that their indices are 0, 1, 2, 3, and 4. This gives us a useful piece of information: the last index in the list is always equal to the length of the list minus 1. This means that no matter how long the list is, there is an easy way to access the last element in the list:

>>> fruits[len(fruits)-1]
'grape'

Python find the index of a single element in a list

To find the index of a single element in a list, use the index() method. Let’s take the fruits list as an example to get the index of one of the elements, as shown below.

>>> fruits=["apple","cherry","banana","orange","grape"]
>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']
>>> fruits.index("banana")
2
>>> fruits.index("grape")
4

We use fruits.index(“banana”) and fruits.index(“grape”) to get the index positions of the elements “banana” and “grape”.

Since the index of the “banana” element in the list is 2, fruits.index(“banana”) returns 2. The index of the “grape” element in the list is 4, so fruits.index(“grape”) returns 4.

Python uses the sort() method to sort a list

If you want to sort the elements in the list, you can use the sort() method. Let’s take the fruits list as an example. Suppose you want to arrange the elements in the list in alphabetical order, as shown below.

>>> fruits=["apple","cherry","banana","orange","grape"]
>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']
>>> fruits.sort()
>>> print(fruits)
['apple', 'banana', 'cherry', 'grape', 'orange']

As you can see, the position of the elements in the list has changed, and now it is in the order of the first letter of the elements.

It should be noted here that when we use the sort() method to sort the list elements, the arrangement order of the elements is completely changed, and there is no way to restore the original order.

If we want to arrange the elements in the list in reverse alphabetical order, we just need to add reverse=True to the parentheses of the sort() method. This is the parameter we pass to the method to tell the method to be in reverse order. Arrange elements. Or take the fruits list as an example, the code is as follows:

>>> fruits.sort(reverse=True)
>>> print(fruits)
>>> print(fruits)
['orange', 'grape', 'cherry', 'banana', 'apple']

Tip: The sort method accepts two parameters, but these two parameters can only be passed by keyword.

The first parameter key, specifies the function to be called on each list element before the comparison, it accepts one parameter and returns a key used to sort the list; its default value is None, which means that it is not correct before each comparison and sorting Comparing items does nothing.

The second parameter, reverse, indicates whether to arrange in reverse order; its default value is False, indicating that it is arranged in order.

The related concepts of this method are relatively abstract. Here, readers only need a brief understanding. Later, we will introduce what are parameters, what are functions, and will see usage examples of the sort method.

Python uses the reverse() method to reverse a list

In addition to sorting the list elements with the sort() method, we can also use the reverse() method to reverse the elements in the list.

Still take the fruits list as an example, assuming the initial order of elements is [‘apple’, ‘cherry’, ‘banana’, ‘orange’, ‘grape’] , through the reverse() method, we can convert the elements in the list into The order is all reversed and becomes [‘grape’, ‘orange’, ‘banana’, ‘cherry’, ‘apple’]. The code looks like this:

>>> fruits=["apple","cherry","banana","orange","grape"]
>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']
>>> fruits.reverse()
>>> print(fruits)
['grape', 'orange', 'banana', 'cherry', 'apple']

Leave a Comment

CONTENTS