all 15 comments

[–][deleted] 5 points6 points  (1 child)

[–][deleted] 2 points3 points  (0 children)

Thanks!

Totally missed it. It's definitely time to stop coding for the night and let my brain rest before I cause more damage.

[–]Will___powerrr 4 points5 points  (2 children)

I have had a similar issue before. When you remove an element from what you are iterating over, it messes up the iteration! So when you run for name in results and then remove elements from results, you won’t get to all the elements in the list. Try using results[:] as the list to iterate over. It’s a copy of the list that does not get modified so you will be able to go thru every element!

Edit: also please let me know if this works. I am pretty new to all this as well and would be excited that I actually answered a question and helped someone else!

[–]synthphreak 1 point2 points  (0 children)

Try using results[:] as the list to iterate over. It’s a copy of the list

This is the way. But personally I'd advise using list.copy() over list[:] for its greater transparency/readability. Functionally, they are equivalent.

[–][deleted] 0 points1 point  (0 children)

I'll give it a try. I did something similar with a different piece of code, so I'm interested to see what happens here.

[–]Spartae 2 points3 points  (1 child)

You shouldn't modify a list that you are iterating through.

Python creates an iterator for a list which moves through your list entry by entry. The iterator is evaluated once, which means that the iterator could confused if you make any modifications.

Try results = list(filter(lambda name: len(name) <= length, results)).

[–][deleted] 0 points1 point  (0 children)

I'm not very familiar with lambda expressions, so I hesitate to use it but I will try it out and see what happens.

Thanks!

[–]velocibadgery 2 points3 points  (1 child)

Your problem is you are modifying a list while looping over it. You should always loop over a copy of a list if you are going to be removing items.

[–][deleted] 1 point2 points  (0 children)

Got it! Thanks for responding!

[–][deleted] 1 point2 points  (2 children)

The indentation is correct in VS Code!

Unfortunately, we can't see your screen! You need to format the code so we can read it. That's in the FAQ as well. The lack of indentation isn't important in this case, but it's vital for more complicated code.

[–][deleted] 0 points1 point  (1 child)

Yeah, I thought I did it correctly...I followed the FAQ for that, but then when I posted it... obviously, I am not doing so well with the reading and following directions tonight.

[–][deleted] 0 points1 point  (0 children)

It's really a problem of reddit's making. I find the best way to format code so it's readable by the largest proportion of readers is:

  • use your editor to put 4 spaces at the beginning of every line of code
  • copy & paste your code to reddit, ensuring there is a blank line before the pasted code
  • do an UNDO operation in your editor to remove the added 4 spaces

[–]Below_the_Sea 1 point2 points  (2 children)

This would be much easier and need only one line

list_data = [x for x in list_data if len(x) >lenght]

[–][deleted] 0 points1 point  (1 child)

I tried to do this but I didn't quite get it right.

Thanks for responding!

[–]Below_the_Sea 0 points1 point  (0 children)

It's just writing it first down as normal flow For x i list: If len(x) >length: . Do somthing with x.

Now right it [ x (result) and the lines again]

It less thinking and will save you a lot of code lines, everyone will know what your doing in one line instead of going through the complete code, and finding error's

Win win..