Python Forum
weird result trying to remove numbers from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
weird result trying to remove numbers from a list
#7
(Aug-26-2019, 11:12 PM)Exsul Wrote: In general, when do you use isinstance() and when do you use ==?

They accomplish quite a different tasks:

- == compares values of two objects for equality Documentation >> The Python Language Reference >> 6. Expressions >> 6.10.1 Value Comparisons
- isinstance([i]object, classinfo[/i]) checks whether object is instance of class (or subclass) (instance of class is fancy way to say what type the object is - int, float, list etc).

You use former if you need to check equality:

>>> a = 42
>>> a == 42
True
>>> a == 43
False


You use latter for identity. As in this task - you want to eliminate all integers regardless of the value, so you check whether object is integer and act accordingly.

>>> a = 42
>>> isinstance(a, int)
True
>>> isinstance(a, float)
False
>>> isinstance(a, (int, float))
True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: weird result trying to remove numbers from a list - by perfringo - Aug-27-2019, 05:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 453 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,719 Jan-05-2024, 08:30 PM
Last Post: sgrey
  regex findall() returning weird result Radical 1 653 Oct-15-2023, 08:47 PM
Last Post: snippsat
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,252 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,723 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,344 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,528 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  set.difference of two list gives empty result wardancer84 4 1,513 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Divide a number by numbers in a list. Wallen 7 8,057 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Remove empty keys in a python list python_student 7 3,051 Jan-12-2022, 10:23 PM
Last Post: python_student

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020