While trying to modify How can I compare string inside a tuple that is in a list suggestion to fit my purpose with the following:
<code>
m = {'User1': [('First', 5),
('Second', 4),
('Fourth', 4),
('Fourth', 5),
('Second', 5)],
'User2': [('Third', 3),
('Third', 2),
('Second', 1),
('First', 1),
('First', 3)]}
# for key, val in m.items():
# print(type(val))
result = {}
for key, val in m.items():
for place, points in val:
result.setdefault(place, []).append(points)
result = [(place, points) for place, points in result.items()]
print(result)
</code>
<code>
m = {'User1': [('First', 5),
('Second', 4),
('Fourth', 4),
('Fourth', 5),
('Second', 5)],
'User2': [('Third', 3),
('Third', 2),
('Second', 1),
('First', 1),
('First', 3)]}
# for key, val in m.items():
# print(type(val))
result = {}
for key, val in m.items():
for place, points in val:
result.setdefault(place, []).append(points)
result = [(place, points) for place, points in result.items()]
print(result)
</code>
m = {'User1': [('First', 5),
('Second', 4),
('Fourth', 4),
('Fourth', 5),
('Second', 5)],
'User2': [('Third', 3),
('Third', 2),
('Second', 1),
('First', 1),
('First', 3)]}
# for key, val in m.items():
# print(type(val))
result = {}
for key, val in m.items():
for place, points in val:
result.setdefault(place, []).append(points)
result = [(place, points) for place, points in result.items()]
print(result)
which works fine on the first dictionary key, but when the loop goes to the
next key I get a
AttributeError: 'list' object has no attribute 'setdefault'
without the m dictionary key being in the final result. What am I missing that is
causing the key to not be generated with the resulting dictionary? The expected output
should be:
<code>m = {'User1': [
('First', [5]),
('Second', [4, 5]),
('Fourth', [4, 5])
],
'User2': [
('Third', [3, 2]),
('Second', [1]),
('First', [1, 3])
]
}
</code>
<code>m = {'User1': [
('First', [5]),
('Second', [4, 5]),
('Fourth', [4, 5])
],
'User2': [
('Third', [3, 2]),
('Second', [1]),
('First', [1, 3])
]
}
</code>
m = {'User1': [
('First', [5]),
('Second', [4, 5]),
('Fourth', [4, 5])
],
'User2': [
('Third', [3, 2]),
('Second', [1]),
('First', [1, 3])
]
}
1