حذف آیتم(Remove Item)

برای حذف یک مورد از یک مجموعه، از remove() یا discard().




مثال


Remove "banana" by using the remove()
method:



thisset = {"apple", "banana", "cherry"}


thisset.remove("banana")


print(thisset)





توجه: اگر موردی برای حذف وجود نداشته باشد، remove() خطایی ایجاد می‌کند.





مثال


Remove "banana" by using the discard()
method:



thisset = {"apple", "banana", "cherry"}


thisset.discard("banana")


print(thisset)





توجه: اگر موردی برای حذف وجود نداشته باشد، discard()
نه خطایی ایجاد نکنید.




همچنین می‌توانید از روش pop() برای حذف استفاده کنید
یک آیتم، اما این روش آخرین مورد را حذف می کند. به یاد داشته باشید که مجموعه
بدون ترتیب هستند، بنابراین شما نمی دانید چه موردی حذف می شود.


مقدار برگشتی روش pop() برابر است
مورد حذف شد.




مثال


Remove the last item by using the pop()
method:



thisset = {"apple", "banana", "cherry"}


x =
thisset.pop()

print(x)


print(thisset)





توجه: مجموعه‌ها نامرتب هستند، بنابراین هنگام استفاده از روش pop()،
شما نمی دانید کدام مورد حذف می شود.






مثال


The clear()
method empties the set:



thisset = {"apple", "banana", "cherry"}


thisset.clear()


print(thisset)





مثال


The del keyword will delete the set
completely:



thisset = {"apple", "banana", "cherry"}


del
thisset


print(thisset)