datacarpentry / python-ecology-lesson

Data Analysis and Visualization in Python for Ecologists
https://datacarpentry.org/python-ecology-lesson
Other
162 stars 308 forks source link

Fixing confusion for indexing python dictionaries #370

Closed Benfeitas closed 4 years ago

Benfeitas commented 5 years ago

See https://github.com/datacarpentry/python-ecology-lesson/issues/360

pr4deepr commented 5 years ago

Not sure if there is a confusion actually. The code: translation = {'one': 1, 'two': 2} and rev = {1: 'one', 2: 'two'} illustrate that both strings (one and two) and numeric types (1 and 2) are acceptable as keys..

Benfeitas commented 5 years ago

@pr4deepr I agree with you. However, learners may think that indexing may be done in terms of key position and key order is not guaranteed to be preserved - aside from insertion order. For the lesson at hand, I think that https://github.com/datacarpentry/python-ecology-lesson/issues/360#issue-407755008 makes sense and is simpler if we just have it as text.

pr4deepr commented 5 years ago

Thanks @RBenRocks . Agree with that, but I think this bit should not be modified, but you can add these changes later on instead? Towards the end of the 'dictionary' section, the issue you raise is addressed: It is important to note that dictionaries are “unordered” and do not remember the sequence of their items (i.e. the order in which key:value pairs were added to the dictionary). Because of this, the order in which items are returned from loops over dictionaries might appear random and can even change with time. But, I think adding a line of code to address this would be good as it is an important concept.

maxim-belkin commented 5 years ago

I've been thinking about this PR and issue #360 and couldn't understand their relationship because the issue says:

When introducing a dictionary, numeric keys are used.

and this PR changes values and, moreover, the very first dictionary introduced does not actually use numeric keys. Now I understand that we're talking about the rev dictionary...

I support this change because we shouldn't be using numeric keys with dictionaries, actually:

a = {1 : '2'}
print(a)
a[1.0] = '3'
print(a)
a[True] = '4'
print(a)

And we should definitely fix that order in which key:value pairs were added to the dictionary part because it is preserved in Python since recently.

Also, statement You can think about a key as a name for or a unique identifier for a set of values in the dictionary. is not correct as there is one-to-one correspondence between keys and values, not one-to-many.

pr4deepr commented 5 years ago

Thanks for clarifying this. Didn't realise the order was conserved recently..

wrightaprilm commented 5 years ago

I like this. I've made one comment on the challenge. If you agree, go ahead and add that to this PR, if not just let me know.

maxim-belkin commented 4 years ago

A lot of things have changed since we discussed this PR. I will revive it with April's suggestions.

maxim-belkin commented 4 years ago

Superseded by #445