Python Forum
Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV
#11
please, both of you, note that

if market['IsActive'] == True  and  market['IsRestricted'] == False:
is better as
if market['IsActive'] and not market['IsRestricted']:
market['IsActive'] and market['IsRestricted'] are booleans, use it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
@buran:
Too dangerous, because I am programming in a lot of different languages
and I have learnt:
if m == true:
is better or less dangerous then
if m:
Reply
#13
(May-28-2019, 01:25 PM)buran Wrote: please, both of you, note that

if market['IsActive'] == True  and  market['IsRestricted'] == False:
is better as
if market['IsActive'] and not market['IsRestricted']:
market['IsActive'] and market['IsRestricted'] are booleans, use it

That's true. I did notice they were booleans and that surprised me. But for me being an inept new Python user, I think I would rather actually put in the unneeded True and False just to remind myself.
Reply
#14
Have a good day.
Reply
#15
when in Rome, do as the Roman's do...
PEP8 recommendation is clear in this respect:

Don't compare boolean values to True or False using ==.

Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
I think that's really valuable and many people don't recognize that:
Quote:A Foolish Consistency is the Hobgoblin of Little Minds
(...)
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.

However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!
Reply
#17
@michalmonday - don't wont to start PEP war.
The point of the quote is - if you are joining developed (already in progress) project - consistency has precedent over PEP recommendations. In this case there is nothing to be consistent with...

And also, quote the full section

Quote:In particular: do not break backwards compatibility just to comply with this PEP!

Some other good reasons to ignore a particular guideline:

When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP.
To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style).
Because the code in question predates the introduction of the guideline and there is no other reason to be modifying that code.
When the code needs to remain compatible with older versions of Python that don't support the feature recommended by the style guide.

nothing of this apply...

In this case we have a snippet that is not part of developed project. @heiner just brings in idiom that they find better in other languages. OP is a relatively new to the language. Given all of this it's better to point out to them what python community consider best practice. It's up to them to decide if they want to follow or not. In long term (if they stick with the language) they will find PEP8 is de-facto considered community standard (i.e. if they follow it's more likely to be consistent when contribute to new and existing projects, than not)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#18
I'd say that the point of the quote is that the consistency and complaince with the style guide is not always the most important, the title emphasizes that.

(May-28-2019, 02:03 PM)buran Wrote: It's up to them to decide if they want to follow or not. In long term (if they stick with the language) they will find PEP8 is de-facto considered community standard (i.e. if they follow it's more likely to be consistent when contribute to new and existing projects, than not)
I completely agree, I think that is why your first post mentioning this matter was very useful, the 2nd post on the other side was kinda putting pressure on people (ignoring the points they made) trying to make them foolishly follow the suggested style.

PEP8 Wrote:Some other good reasons to ignore a particular guideline:
When applying the guideline would make the code less readable
Readability is subjective, at least 1 of the guys mentioned that it would be less readable to him if he didn't use "== True". Seeing that it would be wise to inform the guy about potential negatives of this approach (e.g. writing code that is less readable to other people), rather than telling him "do like we do".
Reply
#19
(May-28-2019, 02:23 PM)michalmonday Wrote: the 2nd post on the other side was kinda putting pressure on people (ignoring the points they made) trying to make them foolishly follow the suggested style
I don't make them to foolishly follow anything. In my posts I say "is better" and I say what PEP8 recommendation is...
On the other hand (for benefit of other users who may read this thraed) I cannot just skip the remark that community recommendation is "too dangerous"...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#20
Ops. Wrong Thread
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,439 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Dictionary in a list bashage 2 494 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,027 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  search a list or tuple for a specific type ot class Skaperen 8 1,853 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,900 Apr-28-2022, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020