-1

I believe, in terms of memory it is not so sufficient, you can leave it to Garbage Collector for sure

array = [.....] 
# just some common comprehension
a = dict((k, v) for k, v in some_dict.iteritems() if v > 1)
# and another some logic which uses *a* object above
b = dict(zip(array, a.keys()))

Here, a is referred only once, and without creating such as disposable object:

b = dict(zip(array, [k for k, v in some_dict.iteritems() if v > 1]))

Please advise what more important factor is: readability vs memory..

Novitoll
  • 770
  • 7
  • 21
  • 2
    Possible duplicate of http://stackoverflow.com/questions/183201/should-a-developer-aim-for-readability-or-performance-first – Moon Cheesez Jun 16 '16 at 05:08
  • Why did you create `a` as a dict if you're only going to use its keys? It's not clear to me what you're asking. Both of your examples create intermediate objects. – BrenBarn Jun 16 '16 at 05:08
  • @BrenBarn, agree - bad example, but the question is given in header.. it explains itself – Novitoll Jun 16 '16 at 05:12
  • In that case your question is too opinion-based. – BrenBarn Jun 16 '16 at 05:13
  • @BrenBarn, agree. I thought, if you create another object -- CPython creates another space in memory with its ID for it, and it may be useless, if you refer to this object only once.. – Novitoll Jun 16 '16 at 05:17
  • 1
    Go for the oneliner and laugh when people beg you to explain it. It was hard to write, it should be hard to read as well. – Matteo Italia Jun 16 '16 at 05:27

1 Answers1

1

My strong opinion is that you should "be reasonable", and how to do that depends on your use case.

If you are building something "to scale" and for use across a large distributed infrastructure, You should create something both readable, and efficient.

However, if you're making a one-off script to run on your personal workstation that will run single-threaded and as-needed, efficiency doesn't matter as much.

Overall, it is best practice to always use good readability practices - the code is much easier to debug this way. Google has a great style guide that you can follow: https://google.github.io/styleguide/pyguide.html


Personally, I think your code is readable as-is.

AbrahamB
  • 1,188
  • 6
  • 12
  • Thanks, for Google Styleguide, I will definitely review it. I probably will need to read more about CPython GC, and how it works to avoid my concerns – Novitoll Jun 16 '16 at 05:13