Nov-28-2023, 12:17 AM
Please use bbtags when posting code.
Here is one way
Here is one way
# Create a list alist = [i for i in range(50)] sorted(alist) print(alist) print() # Create a copy of the list. # Should not try to edit a list that you are iterating over alist_copy = alist.copy() # Iterate over the copy and remove anything above chosen number # from original list for index, number in enumerate(alist_copy): if number > 20: alist.remove(number) print(alist)Output
Output:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts