Skip to content

Show value in error of list.remove #96844

Closed

Description

@tdegeus

Consider

foo = ["a", "b"]
for i in ["c"]:
    foo.remove(i)

This throws with a useless error message:

Traceback (most recent call last):
  File "/Users/tdegeus/Downloads/t.py", line 3, in <module>
    foo.remove(i)
ValueError: list.remove(x): x not in list

Instead it would be very helpful to show the value of x. Now one needs extra manual debugging, or extra lines of code.

Linked PRs

Activity

  1. pochmann commented on Sep 15, 2022

    @pochmann
    Contributor

    What if x is large, so large that it pushes the previous error-information off-screen or even out of the screen buffer?

    What if the conversion of x to string takes a long time or causes another error? (Think of the current large int issue).

    Maybe it could be done for known small good types and values.

    Then again, for example set.remove already does this:


    x = 1; {0}.remove(x) shows:

    KeyError: 1
    

    x = 10**1000000; {0}.remove(x) takes a long time and then prints a lot (testing with Python version without the new string conversion limit).


    class C:
        def __repr__(self):
            raise Exception('foobar')
    {0}.remove(C())
    

    shows:

    KeyError: <exception str() failed>
    
  2. ericvsmith commented on Sep 15, 2022

    @ericvsmith
    Member

    Wouldn't we already have this problem with KeyError on dict lookups:

    >>> {}[1234]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 1234

    ?
    I guess you could argue that the value should be removed from KeyError.

  3. added 2 commits that reference this issue on Jul 5, 2023
  4. corona10 commented on Jul 5, 2023

    @corona10
    Member
    
    Python 3.13.0a0 (heads/main:217f47d6e5, Jul  6 2023, 07:20:36) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> foo = ["a", "b"]
    >>> for i in ["c"]:
    ...     foo.remove(i)
    ...
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    ValueError: 'c' is not in list
    

    Now the error message is updated, here is the rationale why I agree with the updating error message even if for the possibility of large input.

    #106455 (comment)

    cc @tdegeus @pochmann @ericvsmith

  5. added 5 commits that reference this issue on Mar 18, 2024
  6. added a commit that references this issue on Mar 25, 2024
  7. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions