Hướng dẫn can you pop an element from a list in python? - bạn có thể bật một phần tử từ danh sách trong python không?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Python Danh sách pop () với sự trợ giúp của các ví dụ.

Phương thức

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1 loại bỏ mục tại chỉ mục đã cho khỏi danh sách và trả về mục đã xóa.

Thí dụ

# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]

# remove the element at index 2
removed_element = prime_numbers.pop(2)

print('Removed Element:', removed_element)
print('Updated List:', prime_numbers)

# Output: 
# Removed Element: 5
# Updated List: [2, 3, 7]


Cú pháp của danh sách pop ()

Cú pháp của phương thức

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1 là:

list.pop(index)

tham số pop ()

  • Phương thức
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    1 có một đối số duy nhất (chỉ mục).
  • Đối số được truyền cho phương pháp là tùy chọn. Nếu không được thông qua, chỉ mục mặc định -1 được truyền dưới dạng đối số (chỉ mục của mục cuối cùng).-1 is passed as an argument (index of the last item).
  • Nếu chỉ mục được truyền vào phương thức không nằm trong phạm vi, nó sẽ ném IndexError: Pop Index ra khỏi phạm vi ngoại lệ.IndexError: pop index out of range exception.

Trả về giá trị từ pop ()

Phương thức

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1 trả về mục có mặt tại chỉ mục đã cho. Mục này cũng bị xóa khỏi danh sách.


Ví dụ 1: Mục pop tại chỉ mục đã cho từ danh sách

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)

Đầu ra

Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']

Lưu ý: Chỉ mục trong Python bắt đầu từ 0, không phải 1. Index in Python starts from 0, not 1.

Nếu bạn cần bật phần tử thứ 4, bạn cần chuyển 3 cho phương thức

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1.3 to the
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1 method.


Ví dụ 2: pop () không có chỉ mục và cho các chỉ số âm

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 

print('Return Value:', languages.pop())

print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

print('Return Value:', languages.pop(-1))

print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

print('Return Value:', languages.pop(-3))

print('Updated List:', languages)

Đầu ra

When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']

Lưu ý: Chỉ mục trong Python bắt đầu từ 0, không phải 1.

Nếu bạn cần bật phần tử thứ 4, bạn cần chuyển 3 cho phương thức

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1.

Trong Python, sử dụng các phương thức

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
8,
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1 và
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
0 để xóa các mục (phần tử) khỏi danh sách. Cũng có thể xóa các mục bằng cách sử dụng câu lệnh
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
6 bằng cách chỉ định một vị trí hoặc phạm vi có chỉ mục hoặc lát cắt.

  • Xóa tất cả các mục:
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    8
  • Xóa một mục theo chỉ mục và nhận được giá trị của nó:
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    1
  • Xóa một mục theo giá trị:
    Return Value: French
    Updated List: ['Python', 'Java', 'C++', 'C']
    0
  • Xóa các mục bằng chỉ mục hoặc lát:
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    6
  • Xóa các mục đáp ứng điều kiện: Danh sách toàn diện

Xem bài viết sau đây về cách thêm một mục vào danh sách.

  • Thêm một mục vào danh sách trong Python (Phụ lục, mở rộng, chèn)

Xóa tất cả các mục: # programming languages list languages = ['Python', 'Java', 'C++', 'French', 'C'] # remove and return the 4th item return_value = languages.pop(3) print('Return Value:', return_value) # Updated List print('Updated List:', languages)8

Xóa một mục theo chỉ mục và nhận được giá trị của nó:

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1

l = [0, 1, 2]

l.clear()
print(l)
# []

Xóa một mục theo chỉ mục và nhận được giá trị của nó: # programming languages list languages = ['Python', 'Java', 'C++', 'French', 'C'] # remove and return the 4th item return_value = languages.pop(3) print('Return Value:', return_value) # Updated List print('Updated List:', languages)1

Xóa một mục theo giá trị:

Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
0

Xóa các mục bằng chỉ mục hoặc lát:

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
6

l = [0, 10, 20, 30, 40, 50]

print(l.pop(0))
# 0

print(l)
# [10, 20, 30, 40, 50]

print(l.pop(3))
# 40

print(l)
# [10, 20, 30, 50]

Xóa các mục đáp ứng điều kiện: Danh sách toàn diện

print(l.pop(-2))
# 30

print(l)
# [10, 20, 50]

Xem bài viết sau đây về cách thêm một mục vào danh sách.

print(l.pop())
# 50

print(l)
# [10, 20]

Thêm một mục vào danh sách trong Python (Phụ lục, mở rộng, chèn)

list.pop(index)
0

Bạn có thể xóa tất cả các mục khỏi danh sách với

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
8.

  • Bạn có thể xóa mục tại vị trí được chỉ định và nhận giá trị của nó với
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    1.

Chỉ số ở đầu là

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 

print('Return Value:', languages.pop())

print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

print('Return Value:', languages.pop(-1))

print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

print('Return Value:', languages.pop(-3))

print('Updated List:', languages)
0 (lập chỉ mục dựa trên không).

  • Bạn có thể sử dụng các giá trị âm để chỉ định vị trí từ cuối. Chỉ số ở cuối là
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'Ruby', 'C']
    
    # remove and return the last item
    print('When index is not passed:') 
    

    print('Return Value:', languages.pop())

    print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

    print('Return Value:', languages.pop(-1))

    print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

    print('Return Value:', languages.pop(-3))

    print('Updated List:', languages)
    1.

Xóa một mục theo giá trị: Return Value: French Updated List: ['Python', 'Java', 'C++', 'C']0

Xóa các mục bằng chỉ mục hoặc lát:

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
6

list.pop(index)
1

Xóa các mục đáp ứng điều kiện: Danh sách toàn diện

list.pop(index)
2

Xem bài viết sau đây về cách thêm một mục vào danh sách.

Thêm một mục vào danh sách trong Python (Phụ lục, mở rộng, chèn)

list.pop(index)
3

Bạn có thể xóa tất cả các mục khỏi danh sách với

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
8.

  • Bạn có thể xóa mục tại vị trí được chỉ định và nhận giá trị của nó với
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    1.

Xóa các mục bằng chỉ mục hoặc lát: # programming languages list languages = ['Python', 'Java', 'C++', 'French', 'C'] # remove and return the 4th item return_value = languages.pop(3) print('Return Value:', return_value) # Updated List print('Updated List:', languages)6

Xóa các mục đáp ứng điều kiện: Danh sách toàn diện

Xem bài viết sau đây về cách thêm một mục vào danh sách.

list.pop(index)
4

Thêm một mục vào danh sách trong Python (Phụ lục, mở rộng, chèn)

list.pop(index)
5

Bạn có thể xóa tất cả các mục khỏi danh sách với

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
8.

list.pop(index)
6

Bạn có thể xóa mục tại vị trí được chỉ định và nhận giá trị của nó với

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1.

list.pop(index)
7

Chỉ số ở đầu là

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 

print('Return Value:', languages.pop())

print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

print('Return Value:', languages.pop(-1))

print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

print('Return Value:', languages.pop(-3))

print('Updated List:', languages)
0 (lập chỉ mục dựa trên không).

  • Bạn có thể sử dụng các giá trị âm để chỉ định vị trí từ cuối. Chỉ số ở cuối là
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'Ruby', 'C']
    
    # remove and return the last item
    print('When index is not passed:') 
    

    print('Return Value:', languages.pop())

    print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

    print('Return Value:', languages.pop(-1))

    print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

    print('Return Value:', languages.pop(-3))

    print('Updated List:', languages)
    1.

Xóa các mục đáp ứng điều kiện: Danh sách toàn diện

Xem bài viết sau đây về cách thêm một mục vào danh sách.

Thêm một mục vào danh sách trong Python (Phụ lục, mở rộng, chèn)

  • Bạn có thể xóa tất cả các mục khỏi danh sách với
    # programming languages list
    languages = ['Python', 'Java', 'C++', 'French', 'C']
    
    

    # remove and return the 4th item return_value = languages.pop(3)

    print('Return Value:', return_value) # Updated List print('Updated List:', languages)
    8.

Bạn có thể xóa mục tại vị trí được chỉ định và nhận giá trị của nó với

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1.

Chỉ số ở đầu là

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 

print('Return Value:', languages.pop())

print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

print('Return Value:', languages.pop(-1))

print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

print('Return Value:', languages.pop(-3))

print('Updated List:', languages)
0 (lập chỉ mục dựa trên không).

list.pop(index)
8

Bạn có thể sử dụng các giá trị âm để chỉ định vị trí từ cuối. Chỉ số ở cuối là

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 

print('Return Value:', languages.pop())

print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

print('Return Value:', languages.pop(-1))

print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

print('Return Value:', languages.pop(-3))

print('Updated List:', languages)
1.

  • Nếu đối số bị bỏ qua, mục cuối cùng sẽ bị xóa.

Chỉ định một chỉ mục không tồn tại làm tăng lỗi.

list.pop(index)
9

Xem bài viết sau đây để biết ví dụ cho một danh sách các chuỗi.

  • Trích xuất và thay thế các yếu tố đáp ứng các điều kiện của một danh sách các chuỗi trong Python

Nếu bạn muốn loại bỏ các yếu tố trùng lặp, hãy sử dụng

l = [0, 1, 2]

l.clear()
print(l)
# []
7.

  • Xóa/Trích xuất các yếu tố trùng lặp khỏi danh sách trong Python

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0

Bạn có thể xuất hiện từ một danh sách trong Python?

Danh sách Pop trong Python là một hàm được xác định trước, được xây dựng, loại bỏ một mục tại chỉ mục được chỉ định khỏi danh sách.Bạn cũng có thể sử dụng Pop trong Python mà không đề cập đến giá trị chỉ mục.Trong các trường hợp như vậy, hàm pop () sẽ loại bỏ phần tử cuối cùng của danh sách.. You can also use pop in Python without mentioning the index value. In such cases, the pop() function will remove the last element of the list.

Làm thế nào để bạn trích xuất một phần tử từ một danh sách trong Python?

6 cách dễ dàng để trích xuất các yếu tố từ danh sách Python..
Xây dựng vấn đề và tổng quan về giải pháp ..
Phương pháp 1: Sử dụng cắt ..
Phương pháp 2: Sử dụng chỉ số danh sách ..
Phương pháp 3: Sử dụng khả năng hiểu danh sách đơn giản ..
Phương pháp 4: Sử dụng danh sách hiểu với điều kiện ..
Phương pháp 5: Sử dụng liệt kê ..
Phương pháp 6: Sử dụng mảng numpy ().
Summary..

Pop có thể được sử dụng trong danh sách không?

Phương thức pop () loại bỏ mục tại chỉ mục đã cho khỏi danh sách và trả về mục đã xóa..