Python Why won't this python code work?

  • Thread starter Thread starter dmatador
  • Start date Start date
  • Tags Tags
    Code Python Work
AI Thread Summary
The discussion revolves around a Python code snippet that generates a list of odd numbers from 1 to 199 and attempts to remove the number 3 from it. The user encounters an "index out of range" error while iterating through the list. This error occurs because the list's size changes during iteration when an item is removed, leading to invalid index access. The user later acknowledges the mistake and considers the code simple, despite initial confusion. The conversation includes a suggestion that the code appears messy and prompts clarification on the user's intentions.
dmatador
Messages
120
Reaction score
1
lists = [(2 * i + 1) for i in range(100)]

i = 0
while (i < 100):
if(lists == 3):
lists.remove(lists)
i = i + 1

print lists

It is giving me an error about the index being out of range. I am new to python and I am not understanding this error.

Edit: Fixed it. This is by no means messy. Pretty simple stuff. Stupid error on my part.
 
Last edited:
Technology news on Phys.org
dmatador said:
lists = [(2 * i + 1) for i in range(100)]

i = 0
while (i < 100):
if(lists == 3):
lists.remove(lists)
i = i + 1

print lists

It is giving me an error about the index being out of range. I am new to python and I am not understanding this error.


This looks a bit messy. What are you trying to do?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
3
Views
1K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
10
Views
3K
Replies
43
Views
4K
Back
Top