x in alist
is O(n)
, but this code isn't performing membership testing on a list
; words
looks to be a dict
, and membership testing in dict
's keys (or in a set
) is O(1)
(technically, worst case could be O(n)
, but it's average case O(1)
and they put some effort into thwarting even intentional attempts to cause collisions).
This code could be simplified a bit using collections.defaultdict
though, so creating list
s is done implicitly when a non-existent key is looked up:
import collections
words = collections.defaultdict(list)
with open(file_name) as f:
for word in f:
w = word.rstrip()
words[''.join(sorted(w)).lower()].append(w)
If you want uniqueness (though it would lose ordering), you just change to defaultdict(set)
and change append
to add
. If you need uniqueness and ordering, collections.OrderedDict
can (mostly) work as an ordered set
:
import collections
words = collections.defaultdict(collections.OrderedDict)
with open(file_name) as f:
for word in f:
w = word.rstrip()
# True is placeholder, any value will do if you're using in tests properly
words[''.join(sorted(w)).lower()][w] = True