Kinh Nghiệm Hướng dẫn Remove last item in list C# Mới Nhất

Pro đang tìm kiếm từ khóa Remove last item in list C# được Update vào lúc : 2022-12-22 06:09:14 . Với phương châm chia sẻ Bí kíp Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi Read Post vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.

This article will discuss different ways to delete the last element from a list in Python.

Nội dung chính

    Remove the last element from a list in Python using the pop() functionRemove the last element from a list in Python using slicingRemove the last element from a list in Python using del keywordVideo liên quan

Table of Contents

    Remove the last element from a list in Python using the pop() functionRemove the last element from a list in Python using slicingRemove the last element from a list in Python using del keywordSummary

Remove the last element from a list in Python using the pop() function

In Python, the list class provides a function pop(index); it accepts an optional argument index and deletes the element the given index. If no argument is provided, then it, by default, deletes the last element of the list. Lets use this function to remove the last element from a list,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]
# Remove last element from list
list_of_num.pop()
print(list_of_num)

Output:

[51, 52, 53, 54, 55, 56, 57, 58]

As we didnt provide the index argument in the pop() function, therefore it deleted the last item of the list in place.

Advertisements

Remove the last element from a list in Python using slicing

We can slice the list to remove the last element. To slice a list, provide start and end index in the subscript operator. For example,

list[start: end]

It will select the elements from index positions start to end-1. If the start index is not provided, it selects from the first element of the list, and if the end index is not provided, it selects until the end of the list.

If the list has N elements, then slice it to select elements from index position 0 to N-1. In the list, we can also select elements using negative indexing, and the index of the last element in the list is -1. So, to delete the last element from a list, select the elements from start to -1. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]
# Remove last element from list
list_of_num = list_of_num[:-1]
print(list_of_num)

Output:

[51, 52, 53, 54, 55, 56, 57, 58]

It deleted the last element from the list.

Remove the last element from a list in Python using del keyword

To delete the last element from a list, select the last element using negative indexing, and give it to the del keyword to delete it. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]
# Remove last element from list
del list_of_num[-1]
print(list_of_num)

Output:

[51, 52, 53, 54, 55, 56, 57, 58]

It deleted the last item from the list.

Summary

We learned about different ways to delete the last element from a list in Python.

Advertisements

://.youtube/watch?v=wq-bJU0tO9Q

Reply
0
0
Chia sẻ

4369

Review Remove last item in list C# ?

Bạn vừa đọc Post Với Một số hướng dẫn một cách rõ ràng hơn về Video Remove last item in list C# tiên tiến và phát triển nhất

Share Link Tải Remove last item in list C# miễn phí

Bạn đang tìm một số trong những Chia SẻLink Download Remove last item in list C# Free.

Giải đáp vướng mắc về Remove last item in list C#

Nếu sau khi đọc nội dung bài viết Remove last item in list C# vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha
#Remove #item #list