Các hàm trong list Python

Task

Write a program that takes n and a list lst of n elements from the user, sorts all elements in ascending order, and prints the sorted list on the screen.

Example:

  • For n = = 5, lst = [4, 2, 1, 6, 4] the output should be [1, 2, 4, 4, 6]
  • For n = 5, lst = [4, 1, 5, 10, 7] the output should be [1, 4, 5, 7, 10].

Theory

In this lesson, we learn about list methods and functions in Python.

1. The len() function

Return the number of items in a list. Example:

lst = [2, 3, 1]
print(len(lst))

When the above code is compiled and executed, it produces the following result:

3

You can also use len() function for helping to iterate through all of the elements in the list:

lst = [2, 3, 1]
for i in range(len(lst)):
    print(lst[i])

2. The max, min function

These functions are used to return the maximum and minimum element in the list. Example:

lst = [2, 3, 1]
print(max(lst))
print(min(lst))

When the above code is compiled and executed, it produces the following result:

3
1

3. The insert function

It inserts an element to the list at the specified index. Example:

vowels = ['a', 'e', 'i', 'u']
# Chèn xâu 'o' vào vị trí thứ 4 trong list vowels
vowels.insert(3, 'o')
print(vowels)

When the above code is compiled and executed, it produces the following result:

['a', 'e', 'i', 'o', 'u']

4. The remove function

It removes a given object from the list. Example:

lst = ['A', 'B', 'C']
lst.remove('A')
print(lst)

When the above code is compiled and executed, it produces the following result:

['B', 'C']

5. The pop function

It removes the item at the given index from the list and returns the removed item. Example:

lst = ['A', 'B', 'C']
# Remove the second element from the list
lst.pop(1)
print(lst)

When the above code is compiled and executed, it produces the following result:

['A', 'C']

6. The sort function

It sorts the elements of a given list in a specific order. Example:

lst = [4, 5, 3, 7, 6, 1]
# Sorts the elements of a given list in ascending order.
lst.sort()
print(lst)
# Sorts the elements of a given list in descending order.
lst.sort(reverse=True)
print(lst)

When the above code is compiled and executed, it produces the following result:

[1, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 1]

7. The reverse function

It reverses the elements of the list. Example:

lst = [4, 5, 3, 7, 6, 1]
lst.reverse()
print(lst)

When the above code is compiled and executed, it produces the following result:

[1, 6, 7, 3, 5, 4]

8. The count function

It returns the number of occurrences of an element in the given sequence. Example:

lst = [6, 2, 3, 8, 2]
print(lst.count(2))

When the above code is compiled and executed, it produces the following result:

2

9. The clear method

It removes all items from the list. Example:

lst = [1, 2, 3]
lst.clear()
print(lst)

When the above code is compiled and executed, it produces the following result:

[]

Now you can return to the Task and solve it or refer to the instruction below.

Instruction

Code sample not using the built-in functions:

lst = []
n = int(input())

for i in range(n):
    lst.append(int(input()))

for i in range(len(lst)):
    for j in range(i):
        if lst[i] < lst[j]:
            tmp = lst[i]
            lst[i] = lst[j]
            lst[j] = tmp
print(lst)

Code sample using the built-in functions:

lst = []
n = int(input())

for i in range(n):
    lst.append(int(input()))
lst.sort()
print(lst)

Trả lời