-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: Orderdict -> defaultdict(list) #30468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
# TODO: aggspec type: typing.OrderedDict[str, List[AggScalar]] | ||
# May be hitting https://github.com/python/mypy/issues/5958 | ||
# saying it doesn't have an attribute __name__ | ||
aggspec = OrderedDict() | ||
aggspec = defaultdict(list) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change is more complex than this. The code below can be simplified if this is a defaultdict.
Also, is OrderedDict
still being used after this, or can it be removed from the imports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite a few other uses of OrderedDict -> i'm aiming to eliminate most in this issue #30469
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is
OrderedDict
still being used after this, or can it be removed from the imports?
Yes OrderedDIct is still being used.
I am really new to developing (I just greped for the words 'droped' '35' 'support' etc..), can you please explain what is the problem with OrderedDict
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a regular dict:
groupping_values = {}
for value in [1, 3, 1, 1, 2, 3]:
if value not in groupping_values:
groupping_values[value] = [value]
else:
groupping_values[value].append(value)
The if is needed, because when we create the key in the dictionary, we need to create the list with its value. The other times we can append to it.
With an example, if we have an empty dictionary {}
and we want to add the Python programming language, we can do stuff['languages'] = ['python']
. If now that we already have languages
with a list, if we want to add javascript we use a different instruction stuff['languages'].append('javascript')
. This is what the if
in the example does.
Python defaultdict
exists to make things simpler:
import collections
groupping_values = collections.defaultdict(list)
for value in [1, 3, 1, 1, 2, 3]:
groupping_values[value].append(value)
A defaultdict
is "smart", since in the creation we tell its elements will be list. So, when we call the append, it'll create an empty list automatically before. We don't need the if
anymore, and the code is simpler.
This is what it's expected here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for the detailed explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thanks for cleaning this @MomIsBestFriend
Thanks for the detailed explanation, and the patience ;) |
thanks @MomIsBestFriend |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff