List of tuples to dictionary with duplicates keys via list comprehension?
问题 I have a list of tuples with duplicates and I've converted them to a dictionary using this code I found here: https://stackoverflow.com/a/61201134/2415706 mylist = [(a,1),(a,2),(b,3)] result = {} for i in mylist: result.setdefault(i[0],[]).append(i[1]) print(result) >>> result = {a:[1,2], b:[3]} I recall learning that most for loops can be re-written as comprehensions so I wanted to practice but I've failed for the past hour to make one work. I read this: https://stackoverflow.com/a/56011919