7 common list functions in Python

7 common list functions in Python


Lists play an important role in Python's data structures and algorithms. They play an important role and it is almost impossible to ignore them in a project.

Due to the importance of lists, Python provides some built-in functions to perform common list operations. These functions are designed to help developers work faster and easier.

1. len() function

Use the len() function to get the number of items in a list. Here is an example:

my_list = [1, 2, 3, "hello", "rice", "code", 1.2]
list_length = len(my_list)

print(list_length) # returns 7

Without the len() function, you would have to calculate the length manually, as in this example using Python's for loop:

my_list = [1, 2, 3, "hello", "linuxmi", "code", 1.2]
count = 0

for i in my_list:
    count += 1

print(count) # returns 7

It's obvious from this example that the len() function saves some code. Make sure to use it when needed.

2. append() function

The append() function adds a new item to the end of the list. A good use is to add an item to a list after your code meets a certain condition. Here's a simple example:

my_list = [1, 2, 3, "hello", "linuxmi", "code", 1.2]
question = input("你喜欢Python吗?: ").lower()
if question == "yes":
    my_list.append("Python是最好的!!") # append()函数的使用
else:
    my_list.append("你应该试试Python") # append()函数的使用
    
print(my_list)

This example uses an if statement to add a sentence to an initial list based on user input.

The **append()** function can only add one item to the list at a time. You can use operators instead of additional functions:

my_list = [1, 2, 3, "hello", "linuxmi", "code", 1.2]
my_list += ["Python是最好的!!"]

Using the addition operator ends up being less efficient since it doesn't modify your initial list. Instead, it creates a new list in memory and adds a new item to it. The append **()** function directly modifies your initial list.

3. extend() function

The **extend() function is a built-in function that adds multiple items to an existing list at once. It accepts a new item as a parameter and uses that parameter to modify the existing list. Here's how to use the extend()** function:

my_list = [1, 2, 3, "hello", "linuxmi", "code", 1.2]
my_list.extend(["yuyu", "linux", 93139])

print(my_list)# prints [1, 2, 3, 'hello', 'linuxmi', 'code', 1.2, 'yuyu', 'linux', 93139]

The **extend()** function can only accept one argument, so you should add all items to a list like the code above.

4. reverse() function

The reverse function simply rewrites your list in reverse order. Here is an example using the reverse function:

my_list = [1, 2, 3, "hello", "linuxmi", "code"]
my_list.reverse()

print(my_list) # prints ['code', 'linuxmi', 'hello', 3, 2, 1]

To reverse a list without using the reverse() function, you need to slice the list. Here is an example:

my_list = [1, 2, 3, "hello", "linuxmi", "code"]
reversed_list = my_list[::-1]

print(reversed_list) # prints ['code', 'linuxmi', 'hello', 3, 2, 1]

In the above example, **my_list[::-1]** creates a reverse copy of the original list. This means there will be two lists in memory, which is not the best approach if you just want to reverse the original list.

5. insert() function

The insert() function modifies the list and adds items to it, just like the append() function. However, the **insert()** function allows you to specify the index (position) of the new item in the list. Here is an example:

my_list = [1, 2, 3, "hello", "linuxmi", "code"]
my_list.insert(0, "first") # 将"first"添加到列表的开头

print(my_list) # prints ['first', 1, 2, 3, 'hello', 'linuxmi', 'code']

From the above code, this is the correct syntax for using the **insert()** function:

your_list.insert(index, new_list_item)

6. sort() function

The **sort()** function takes a list of a specific data type and rearranges the elements in ascending order. Here is an example:

my_list = [1, 2, 10, 30, 3, 2.4]
my_list2 = ['code', 'linuxmi', '93139', 'hello']

my_list.sort()
my_list2.sort()

print(my_list) # prints [1, 2, 2.4, 3, 10, 30]
print(my_list2) # prints ['code', 'hello', 'linuxmi', '93139']

If you use the sort() function on a list with different data types (such as strings and numbers), you will receive a TypeError.

7. count() function

The **count()** function counts the number of times a specific element appears in a list and returns that value to you. How to use it:

my_list = ['code', 10, 30, 'code', 3, 'code', 'linuxmi', 5]

print(my_list.count('code')) # prints 3

Doing something like this without the count() function would require writing more code. Here is an example:

my_list = ['code', 10, 30, 'code', 3, 'code', 'linuxmi', 5]
count_code = 0

for item in my_list:
    if item == 'code':
        count_code += 1
        
print(count_code) # prints 3

The len() function will return the total number of elements in the list, while the count() function will return the number of times a specific element appears in the list.

General catalog of "AUTOSAR lineage decomposition (ETAS tool chain)"

Guess you like

Origin blog.csdn.net/PlutoZuo/article/details/134729948