Skip to content
Advertisement

How do I fill a dictionary with indices in a for loop?

I have a transposed Dataframe tr:

7128 8719 14051 14636
JDUTC_0 2451957.36 2452149.36 2457243.98 2452531.89
JDUTC_1 2451957.37 2452149.36 2457243.99 2452531.90
JDUTC_2 2451957.37 2452149.36 2457244.00 2452531.91
JDUTC_3 NaN 2452149.36 NaN NaN
JDUTC_4 NaN 2452149.36 NaN NaN
JDUTC_5 NaN 2452149.36 NaN NaN
JDUTC_6 1.23 2452149.37 NaN NaN
JDUTC_7 NaN NaN NaN NaN
JDUTC_8 NaN NaN NaN NaN
JDUTC_9 NaN NaN NaN NaN

And I create dict ‘a’ with this block of code:

JavaScript

Which gives me this output:

JavaScript

What I expect dict ‘a’ to look like is this:

JavaScript

What I am doing wrong? Why is a[_] = b overwriting all the previous keys when print(_) is verifying that _ is always the next column label?

Advertisement

Answer

With the correct name convention, I would change your code after:

JavaScript

(people should give minimal working code – but often forget to give e.g. the code to build the data frame etc. and the imports)

to:

JavaScript

continue and pass are not necessary – they just say “go on” with the loop. In Python, you are not forced to give the else branch:

JavaScript

Such collection of data using for-loops are better done with list-comprehensions:

JavaScript

And finally, you can even build list-comprehensions for dictionaries – making this entire code a one-liner – but a readable one – when one is familiar with list comprehensions:

JavaScript

You can see the result:

JavaScript

List-comprehensions are going into the direction of Functional Programming (FP). Which exactly deals with the problem of not to apply mutation (like the b.append() or b.clear() methods – because – as you have seen: your case is a demonstration of how easily a bug is generated when using mutation. – and would contribute to the discussion – why FP – while it at the first sight looks brain-unfriendly – is actually the more brain-friendly way to program.

List comprehensions are the Pythonic form of “map” – and if you use a “if” inside list comprehensions – this is the Pythonic equivalent to “filter” which FP people know like a second brain for breathing.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement