Skip to content
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

Simplify code for collecting + sorting of all possible characters. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jonasreinsch
Copy link

@jonasreinsch jonasreinsch commented Oct 19, 2023

Small code simplification:

the line

chars = sorted(list(set(''.join(words))))

can be simplified to

chars = sorted(set(''.join(words)))

because sorted(...) accepts any iterable and set(...) returns an iterable.

I noticed this in your building makemore video (thank you for that!) at 15:56

@ahasan85
Copy link

@jonasreinsch
Copy link
Author

@jonasreinsch set is unordered https://docs.python.org/3.11/library/stdtypes.html#set-types-set-frozenset

@ahasan85 yes, but sorted then returns an ordered / sorted list.

Just try it out:

>>> sorted(set(list(''.join(['hello', 'my', 'friends']))))
['d', 'e', 'f', 'h', 'i', 'l', 'm', 'n', 'o', 'r', 's', 'y']
>>> sorted(set(''.join(['hello', 'my', 'friends'])))
['d', 'e', 'f', 'h', 'i', 'l', 'm', 'n', 'o', 'r', 's', 'y']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants