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
#3
I would approach it this way:

Take away ambiguity: 'number' is integer.

Make a plan. In spoken language: "give me items in list which are not integers".

Find the pieces:

- iterate over elements: use for-loop
- test whether element is integer: use built-in functions isinstance()
- wrap it into list comprehension

>>> lst = [1, 'one', 2, 'two']
>>> no_ints = [item for item in lst if not isinstance(item, int)]
>>> no_ints
['one', 'two'] 
EDIT: if 'number' is both integers and floats then element testing should be isinstance(item, (int, float))
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-21-2019, 06:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove duplicates from dicts with list values wardancer84 27 6,257 May-27-2024, 04:54 PM
Last Post: wardancer84
  unable to remove all elements from list based on a condition sg_python 3 1,772 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 21,386 Jan-05-2024, 08:30 PM
Last Post: sgrey
  regex findall() returning weird result Radical 1 3,223 Oct-15-2023, 08:47 PM
Last Post: snippsat
  find random numbers that are = to the first 2 number of a list. Frankduc 23 7,599 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 5,927 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 3,862 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 2,970 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  set.difference of two list gives empty result wardancer84 4 2,669 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Divide a number by numbers in a list. Wallen 7 11,276 Feb-12-2022, 01:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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