Skip to content
Advertisement

For loops and conditionals in Python

I am new to Python and I was wondering if there was a way I could shorten/optimise the below loops:

JavaScript

I tried this oneliner, but it doesn’t seem to work:

JavaScript

Advertisement

Answer

You can use itertools.product to handle the nested loops for you, and I think (although I’m not sure because I can’t see your data) you can skip all the .get and .update and just use the [] operator:

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