I have a list comprehension and want to store the results in a set. But, the list is unhashable and therefore can't be stored in a set.
Is there some way to do a tuple comprehension instead?
I have a list comprehension and want to store the results in a set. But, the list is unhashable and therefore can't be stored in a set.
Is there some way to do a tuple comprehension instead?
I came up with the answer in the process of asking the question, so I figured I'd post it with my answer and help the next person to search for a solution. I couldn't find anything about a tuple comprehension, but you can just cast a list to a tuple and store that in the set. Like so:
wave = set() for srces in itertools.combinations(games, self.size): wave.add(tuple([(book, 0) for book in srces])) [ ]. Without them, you'll create a generator expression, which tuple() will convert to a tuple.append to a set.AttributeError: 'set' object has no attribute 'append'.tuple(listcomp) takes up to 2.125 N memory (1.125 for listcomp, then 1 for tuple) and tuple(generator) takes up to 1.25 N memory. The extra 12.5% and 25% for overallocating. So neither list nor tuple appear to grow by allocating a new place and moving the data, I guess the OS happily extends the existing allocation...