Hướng dẫn pop item from list python - mục pop từ python danh sách

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

list.pop(index)
4 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

list.pop(index)
4 là:

list.pop(index)

tham số pop ()

  • Phương thức
    list.pop(index)
    4 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

list.pop(index)
4 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

list.pop(index)
4.3 to the
list.pop(index)
4 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

list.pop(index)
4.

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Danh sách Python () 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)
0 loại bỏ phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách.

Thí dụ

# create a list
prime_numbers = [2, 3, 5, 7, 9, 11]

# remove 9 from the list prime_numbers.remove(9)

# Updated prime_numbers List print('Updated List: ', prime_numbers) # Output: Updated List: [2, 3, 5, 7, 11]


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

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

# 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 là:

list.remove(element)

loại bỏ () tham số

  • 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)
    0 lấy một phần tử duy nhất làm đối số và loại bỏ nó khỏi danh sách.
  • Nếu
    # 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)
    3 không tồn tại, nó sẽ ném valueError: list.remove (x): x không trong ngoại lệ danh sách.ValueError: list.remove(x): x not in list exception.

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

# 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 không trả về bất kỳ giá trị nào (trả về
# 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)
5).


Ví dụ 1: Xóa phần tử khỏi danh sách

# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']

# 'rabbit' is removed animals.remove('rabbit')

# Updated animals List print('Updated animals list: ', animals)

Đầu ra

Updated animals list:  ['cat', 'dog', 'guinea pig']

Ví dụ 2: Remove () phương thức trên danh sách có các phần tử trùng lặp

Nếu một danh sách chứa các phần tử trùng lặp, 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)
0 chỉ xóa phần tử khớp đầu tiên.

list.pop(index)
0

Đầu ra

list.pop(index)
1

Ví dụ 2: Remove () phương thức trên danh sách có các phần tử trùng lặp


Nếu một danh sách chứa các phần tử trùng lặp, 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)0 chỉ xóa phần tử khớp đầu tiên.

list.pop(index)
2

Đầu ra

list.pop(index)
3

Ở đây, chúng tôi đang gặp lỗi vì 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)
7 không chứa
# 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.


  • Nếu bạn cần xóa các phần tử dựa trên chỉ mục (như phần tử thứ tư), bạn có thể sử dụng phương thức pop ().
  • Ngoài ra, bạn có thể sử dụng câu lệnh Python del để xóa các mục khỏi danh sách.

Làm thế nào để bạn bật một mục cụ thể từ một danh sách trong Python?

Bạn có thể sử dụng phương thức pop () để loại bỏ các yếu tố cụ thể của danh sách. Phương thức pop () lấy giá trị chỉ mục làm tham số và xóa phần tử tại chỉ mục được chỉ định. Do đó, A [2] chứa 3 và pop () loại bỏ và trả về giống như đầu ra.use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output.

Bạn có thể bật từ danh sách Python không?

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ả về một giá trị pop từ một danh sách trong Python?

Danh sách Python pop () Phương thức cú pháp..
Cú pháp: list_name.pop (index).
Trả về: Trả về giá trị cuối cùng hoặc giá trị chỉ mục đã cho từ danh sách ..
Ngoại lệ: tăng chỉ mục khi chỉ số nằm ngoài phạm vi ..

Bạn có thể sử dụng pop trong danh sách không?

pop () sẽ trả về phần tử cuối cùng trong danh sách và cũng loại bỏ nó.Gọi phương thức pop () trên một danh sách trống sẽ gây ra lỗi.Bạn có thể chỉ định một chỉ mục của một mục sẽ được trả về và xóa khỏi danh sách.Trong ví dụ trên, các thành phố.. Calling the pop() method on an empty list will raise an error. You can specify an index of an item to be returned and removed from a list. In the above example, cities.