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
#1
I'm trying to remove all the numbers from a list, but when I run the code, I get a strange result:

nums = [1, 2, 3, 4, 5]
words = ["I", "sing", "of", "arms", "and", "a", "man"]
both = nums + words

for each in both:
    if each in range(1000):
        both.remove(each)

print(both)
Output:
[2, 4, 'I', 'sing', 'of', 'arms', 'and', 'a', 'man'] Process finished with exit code 0
What are the 2 and 4 still there?
Reply


Messages In This Thread
weird result trying to remove numbers from a list - by Exsul - Aug-21-2019, 01:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 428 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,635 Jan-05-2024, 08:30 PM
Last Post: sgrey
  regex findall() returning weird result Radical 1 641 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,188 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,693 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,320 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,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  set.difference of two list gives empty result wardancer84 4 1,474 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Divide a number by numbers in a list. Wallen 7 8,019 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Remove empty keys in a python list python_student 7 3,016 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