l = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]
d = {}
for x, y in l:
d.setdefault(x, []).append(y)
print d
produces:
{'a': [1, 2, 3], 'c': [1], 'b': [1, 2]}
More Related Contents:
- Why can’t I use a list as a dict key in python?
- List of tuples to dictionary [duplicate]
- Why can’t I use a list as a dict key in python? Exactly what can and cannot be used, and why?
- In what case would I use a tuple as a dictionary key? [closed]
- How do I convert two lists into a dictionary?
- Remove duplicate dict in list in Python
- How to convert comma-delimited string to list in Python?
- list to dictionary conversion with multiple values per key?
- How to overcome TypeError: unhashable type: ‘list’
- List of dicts to/from dict of lists
- Appending a dictionary to a list – I see a pointer like behavior
- Explicitly select items from a list or tuple
- Check if value already exists within list of dictionaries?
- Convert list into a dictionary [duplicate]
- Transform “list of tuples” into a flat list or a matrix
- Combining Dictionaries Of Lists In Python
- Most Pythonic Way to Build Dictionary From Single List
- Python: get a dict from a list based on something inside the dict
- How to convert nested list of lists into a list of tuples in python 3.3?
- Make dictionary from list with python [duplicate]
- how to convert list of dict to dict
- How do I return dictionary keys as a list in Python?
- convert csv file to list of dictionaries
- What is the meaning of list[:] in this code? [duplicate]
- Iterate over pairs in a list (circular fashion) in Python
- Getting the difference (delta) between two lists of dictionaries
- Python: iterating over list vs over dict items efficiency
- If in Python I put a list inside a tuple, can I safely change the contents of that list? [duplicate]
- How to extract all values from a dictionary in Python?
- Comparing 2 lists consisting of dictionaries with unique keys in python