Python إزالة عنصر قائمة
إزالة عنصر قائمة
توجد عدة طرق لإزالة العناصر من القائمة:
مثال
الطريقة remove()
تزيل العنصر المحدد:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
مثال
الطريقة pop()
تزيل الفهرس المحدد ، (أو العنصر الأخير إذا لم يتم تحديد الفهرس):
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
مثال
الكلمة الأساسية del
تزيل الفهرس المحدد:
thislist = ["apple", "banana", "cherry"]
del
thislist[0]
print(thislist)
مثال
يمكن del
للكلمة الرئيسية أيضًا حذف القائمة تمامًا:
thislist = ["apple", "banana", "cherry"]
del
thislist
مثال
الطريقة clear()
تفرغ القائمة:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)