Reputation: 13
I'm having trouble sorting individual tuple created by a list comprehension. Say we have:
words = [(a, b, c) for a in al for b in bl for c in cl]
Now I want to sort each tuple (a, b, c) by doing:
map(lambda x: sorted(x), words)
which gives me the error: 'tuple' object is not callable.
I also tried:
for i in range(len(words)):
out = [words[i][0], words[i][1], words[i][2]]
print out.sort()
which prints a bunch of Nones.
What am I missing? Thanks in advance.
Upvotes: 1
Views: 875
Reputation: 251353
You could just sort the tuples as part of the creation:
words = [sorted((a, b, c)) for a in al for b in bl for c in cl]
Note that this will give you a list of lists, not a list of tuples, because sorted
returns a list. If you really want tuples you'll have to do
words = [tuple(sorted((a, b, c))) for a in al for b in bl for c in cl]
Upvotes: 4