Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove numbers from a list
#1
I've been working a problem in another forum and ran across something I don't understand.
Giving a list of numbers as input and then removing the numbers that are less than or equal to a boundry,
can't seem to get all numbers to be removed. Any help understanding why the numbers are not removed would be great.
The numbers is usually the second in the list.

boundry = 10
numbers = list(map(int, input('>> ').split(',')))

print(f'list -> {numbers}')

for number in numbers:
    if number <= boundry:
        print(f'removed number -> {number}')
        numbers.remove(number)

print(f'Numbers left -> {numbers}')
Output:
Output:
>> 3,5,8,14,10 list -> [3, 5, 8, 14, 10] removed number -> 3 removed number -> 8 removed number -> 10 Numbers left -> [5, 14]
As you can see, 3,8,10 are removed, but not 5.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
I found a solution.
Had to reverse the list and keep index positions.

boundry = 10
numbers = list(map(int, input('>> ').split(',')))

print(f'list -> {numbers}')

for number in reversed(range(len(numbers))):
    if numbers[number] <= boundry:
        print(f'removed number -> {numbers[number]}')
        del numbers[number]

print(f'Numbers left -> {numbers}')
Output:
>> 3,5,8,14,22,9 list -> [3, 5, 8, 14, 22, 9] removed number -> 9 removed number -> 8 removed number -> 5 removed number -> 3 Numbers left -> [14, 22]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You shouldn't alter a list that you are iterating over, iterate over a copy of the list and alter the original list.
for number in numbers[:]:
Reply
#4
I prefer building a new list that only contains the values I want to keep.
boundry = 10
numbers = list(map(int, input('>> ').split(' ')))
kept = [number for number in numbers if number > boundry]
print('Numbers >', boundry, '=', kept)
jefsummers likes this post
Reply
#5
This is the solution I gave but, just seems there are better ways. Doesn't work if the list is not sorted first. Was trying to keep the original alignment eg(3,6,2,22) so to speak. Think I still get incorrect placement from time to time.

boundry_dict = {} # Create a dict to hold key/value
boundry_num = 10 # Boundry starts at 10
numlist = [] # A list to hold the numbers

numbers = list(map(int, input('>> ').split(','))) # Get input separated by comma
numbers.sort() # Sort the numbers in case not sequenced
# Loop through the input
for number in numbers:
    if number <= boundry_num: # If a number is less than boundry append to list
        numlist.append(number)
    else: # If all numbers less than boundry then increase boundry by 10
        boundry_num += 10
        numlist = [] # Clear the list
        numlist.append(number) if number <= boundry_num else False # Without this it will miss a number
    boundry_dict[boundry_num] = numlist # Add the boundry number and list to dict

# Loop through the dict and display
for key, value in boundry_dict.items():
    print(f'Group of {key}\'s: {value}')
output
Output:
>> 12,5,9,6,22,40,42 Group of 10's: [5, 6, 9] Group of 20's: [12] Group of 30's: [22] Group of 40's: [40] Group of 50's: [42]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 456 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,720 Jan-05-2024, 08:30 PM
Last Post: sgrey
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,255 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,724 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,529 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Divide a number by numbers in a list. Wallen 7 8,059 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Remove empty keys in a python list python_student 7 3,052 Jan-12-2022, 10:23 PM
Last Post: python_student
  producing numbers out of a list bouraque7878 10 3,782 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  Remove an item from a list contained in another item in python CompleteNewb 19 5,782 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  How to change odd to even numbers in the list? plumberpy 8 3,764 Aug-08-2021, 11:07 AM
Last Post: plumberpy

Forum Jump:

User Panel Messages

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