My fight against Python: Chapter #47257

avatar

So, there I was, not knowing enough to ace it. But knowing enough to fail hard.
You Devs will laugh at and, I hope, with me.

We begin our story in my office. some simple program to be written, to filter some objects according to their attributes.
Why outsource it, when me can do it in minutes.
HACK AWAY
After copying a lot of headers and such, I finished a running script.
It does run, it delivers an output and, oh..
ERROR
There are objects in that list, that should still be filtered.

Hmm..Ok,look at the if clause..nope looks exactly as it should be, nothing there to be changed. Lets run it again.
Same Error in output list.

Ah, another idea, why not random shuffle my list!
I have no idea, why I did this at this point. Randomizing does nothing to improve the situation, but I was desperate
Same output, just randomized.
At least this was working.
Randomizing list working? CHECK
Filter list? NO

Let's numb it down for me.

isnochys:~/work$ cp work.py test.py
isnochys:~/work$ vi test.py
#delete all the things I don't need..
print(list)
print(len(list))
filter(list)
print(list)
print(len(list))
isnochys:~/work$ python test.py

Output:

<list>
12456
<list>
6228

I haven't eaten or slept since starting writing the software!
It must have been hours!

Meanwhile I had meetings, where I was zoned out, answering on auto mode, still fixed on that bloody list!

"Isnochys, any question from your side?"
"..um..no, India will deliver."

Is it the naming of objects?
Are their attributes doing something within Python?
What the hell is going on?
Do I need to sacrifice something to the computer gods, again?

The real Devs will be asking, what is he talking about?
Couldn't he just show us the code?
I could, but there would not be any fun story to tell and entertain you.

So, I numbed my test.py file further down.
Let me share it with you.

import random
l = ['a','b','c','d','f']
#random.shuffle(l)
for i in l:
   print(i)
for i in l:
   print(i)
   l.remove(i)
print(l)

Output:

a
b
c
d
f

a
c
f

['b', 'd']

At this point I was about to recompile Python.

giphy4.gif

WAIT!
Dear Lord, what have I done, am I that stupid?

Here is my theory:
Yes, I am that stupid.

for i in list:

is another way for

for x in range(len(list)): 
   i = list[x]

In the first round, we remove the first element.
['a','b','c','d','f']
x=0 Element 0 is 'a'

Remaining list: ['b','c','d','f']
Next round: x=1, but 'b' is now on position 0
On index 1, position 2, there is 'c'
Remaining list: ['b','d','f']
Next round: x=2
On index 2, position 3, there is 'f'
remove that, list finished and we get
['b','d']
as result.
TADA!

Bonus
Does it work the other way around?
Yes!

l = ['a','b','c','d','f']
for i in l:
   l.append(i)

THIS WILL NEVER END!
:)))

How did I solve my issues from..days ago?
Instead of removing items from that list, I just created a new list, changed my if clause and add to the new list the items that shoud stay in the old list.
There is for sure a better way, but I will not look for it.

"Hey, numbnut, why didn't you just google it?"

I have no idea, what to google for.
"python list for loop missing items"
"for loop skip items python"
"bloody hell, that shit doesn't go through all items for python"

Maybe there is a page in the python manual.
But I didn't read the manual, I just do it LIVE!

I should just go back to COBOL or JCL.
Or forward to Terraform.
Oh, maybe I write a bit about that?
Why has my new VM some TB of RAM?
More in the near future.

Vote for me as witness, you are going to miss some beer otherwise!;)



0
0
0.000
6 comments
avatar

!BEER

0
0
0.000
avatar

This would have just put gasoline into the fire.
!BBER

0
0
0.000
avatar

Thank you for your witness vote!
Have a !BEER on me!
To Opt-Out of my witness beer program just comment !STOP below

0
0
0.000