Skip to content

Commit 687d42d

Browse files
Return True if an item was removed, False otherwise.
1 parent 139c277 commit 687d42d

8 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎Doc/builtins/stdtypes.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,8 @@ application).
16881688
.. method:: list.discard(value, /)
16891689

16901690
Remove the first item from the list where ``list[i] == value``
1691-
if such an item is present.
1691+
if such an item is present. Return ``True`` if an item was removed,
1692+
``False`` otherwise.
16921693

16931694
.. versionadded:: next
16941695

‎Doc/tutorial/datastructures.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ of the methods of list objects:
4747
:noindex:
4848

4949
Remove the first item from the list whose value is equal to *value*, if there
50-
is such an item.
50+
is such an item. Return ``True`` if an item was removed, ``False`` otherwise.
5151

5252

5353
.. method:: list.pop(index=-1, /)

‎Doc/whatsnew/3.16.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Other language changes
108108
(Contributed by Kumar Aditya in :gh:`157361`.)
109109

110110
* Added :meth:`list.discard`, which removes the first occurrence of a value
111-
from a list if it is present, like :meth:`set.discard`.
111+
from a list if it is present and returns whether an item was removed.
112112
(Contributed by Stan Ulbrych in :gh:`160724`.)
113113

114114

‎Lib/collections/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ def remove(self, item):
13871387
self.data.remove(item)
13881388

13891389
def discard(self, item):
1390-
self.data.discard(item)
1390+
return self.data.discard(item)
13911391

13921392
def clear(self):
13931393
self.data.clear()

‎Lib/test/list_tests.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,13 @@ def __eq__(self, other):
375375

376376
def test_discard(self):
377377
a = self.type2test([0, 0, 1])
378-
a.discard(1)
378+
self.assertIs(a.discard(1), True)
379379
self.assertEqual(a, self.type2test([0, 0]))
380-
a.discard(0)
380+
self.assertIs(a.discard(0), True)
381381
self.assertEqual(a, self.type2test([0]))
382-
a.discard(0)
382+
self.assertIs(a.discard(0), True)
383383
self.assertEqual(a, self.type2test([]))
384-
a.discard(0)
384+
self.assertIs(a.discard(0), False)
385385
self.assertEqual(a, self.type2test([]))
386386

387387
self.assertRaises(TypeError, a.discard)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Add :meth:`list.discard`, which removes the first occurrence of a value from
2-
a list if it is present.
2+
a list if it is present and returns whether an item was removed.

‎Objects/clinic/listobject.c.h‎

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/listobject.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,16 +3520,19 @@ list.discard
35203520
/
35213521
35223522
Remove first occurrence of value if it is present.
3523+
3524+
Return True if an item was removed, False otherwise.
35233525
[clinic start generated code]*/
35243526

35253527
static PyObject *
35263528
list_discard_impl(PyListObject *self, PyObject *value)
3527-
/*[clinic end generated code: output=4d8c1aee9a101f8c input=e1abe80fdc97f48e]*/
3529+
/*[clinic end generated code: output=4d8c1aee9a101f8c input=6330bc648285d994]*/
35283530
{
3529-
if (list_remove_lock_held(self, value) < 0) {
3531+
int res = list_remove_lock_held(self, value);
3532+
if (res < 0) {
35303533
return NULL;
35313534
}
3532-
Py_RETURN_NONE;
3535+
return PyBool_FromLong(res);
35333536
}
35343537

35353538
static int

0 commit comments

Comments
 (0)