makemore icon indicating copy to clipboard operation
makemore copied to clipboard

Simplify code for collecting + sorting of all possible characters.

Open jonasreinsch opened this issue 2 years ago • 2 comments

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

jonasreinsch avatar Oct 19 '23 14:10 jonasreinsch

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

ahasan85 avatar Oct 19 '23 15:10 ahasan85

@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']

jonasreinsch avatar Oct 19 '23 15:10 jonasreinsch