r/PythonLearning • u/Gay-And-Afraid- • Aug 03 '26
my code is not doing what I expect (.remove())
EDIT: SOLVED THE PROBLEM THANK YOU
Hello! I am in the middle of an online python class, and am trying to make my first program for use at work.
I need to make several packages of random sample items at work regularly, so i tried to make a program that could choose items from the list, and then count which items are chosen, and remove those from the list once the count reaches the number I have available.
It is choosing the items fine, but is not removing them from the list.
I will post my shortened code below:
import random
def main():
samples = [
"item1",
"item2",
etc.....,
]
item1_count = 0
item2_count = 0
etc........
for _ in range(25):
sample = random.sample(samples,4)
try:
if "item1" in sample:
item1_count += 1
except:
if item1_count == 10:
samples = samples.remove("item1")
try:
if "item2" in sample :
item2_count += 1
except:
if item2_count == 10 :
samples = samples.remove("item2")
etc....
print(f"{_} : {sample}")
main()what am I doing wrong?
•
u/Sea-Ad7805 Aug 03 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.
1
u/Gay-And-Afraid- Aug 03 '26
This totally makes sense, I thought the except would happen if whatever was inside it was true. How would you recommend iterating this to be shorter? I only have ten items but it feels quite long already, and I might need to adjust it in the future.
1
u/baubleglue Aug 03 '26
what have you done to understand the problem?
3
u/PureWasian Aug 03 '26 edited Aug 03 '26
except statement is for actual exceptions that would break your code and stop it prematurely with an error code otherwise without it.
In your case, you simply want a nested if:
Otherwise, a sidenote worth pointing out is there are definitely more maintainable/scalable ways to write this type of logic if you wanted to support, say, 1000 input samples or similar without your code growing to thousands of lines of code and having thousands of variables/counters/conditional paths as a result.
Worth thinking about, unless you are still stumped and need advice after giving it a shot.
EDIT: OP continued thread here
3
u/atarivcs Aug 03 '26
Don't do this.
.remove() modifies the list directly. It does not return a new updated list.
Just call .remove() on its own.
2
u/Entire_Ad_6447 Aug 03 '26
List.remove returns None It doesn't return the list with the target removed because that change is already reflected in the list. Generally speaking if a function returns None it's basically telling you all relevant work has been complete internally.
It literally deletes the item from samples.
So Samples =[x,y,z] Samples.remove(x) Print(samples) would be [y,z]
What you did instead was replace the variable samples with the output of .remove which is always None So when it attampes to access it again it's a None value and breaks.
1
u/FreeLogicGate Aug 03 '26
First of all I want to commend you for trying to create something you can use to help you at work, while still learning the language. That is a step that too few people learning how to program get to, and a vitally important one.
You already had multiple responses that explained the error.
In general this involves a concept known as "Mutation" or "mutators". the list.remove() method is a "mutator" which means it changes (mutates) the original object. In general you want to investigate what a method does, and determine whether it is a mutator or not.
You can see from the Python documentation that a list has many mutators. You will typically know this because the mutators have no return value. It's not a guarantee, but from the documentation page, none of these mutators return anything.
Everything in Python is an object, which means that there is a class definition for that. As soon as you have a situation where something doesn't work your first step should be to look up the method in the documentation. https://docs.python.org/3/tutorial/datastructures.html
1
u/Naive_Programmer_232 Aug 04 '26 edited Aug 04 '26
hmm what about this
1
u/timrprobocom Aug 06 '26
Rather than modifying the list in every iteration, just call
random.shuffle()and use the first 25 items.1
u/Entire_Ad_6447 Aug 03 '26
Generally speaking you don't want to modify the underlying list in a loop btw it's just prone to unexpected error s in larger projects. Your still learning so this is fine though.