How can I delete a specific data in Python?

To remove a piece of data, you can use the del keyword followed by the reference of the data you want to delete. Here is an example:

# 创建一个列表
my_list = [1, 2, 3, 4, 5]

# 删除列表中的第三个元素(索引为2)
del my_list[2]

# 打印列表
print(my_list)

The output is:

[1, 2, 4, 5]

Similarly, you can also delete other types of data, such as a key-value pair in a dictionary or a property in an object.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds