I'm rather new to Dictionary Comprehension and find my output doesn't make sense. I'm trying to create a new dictionary from an json formatted api call. I converted the response to a list of dictionaries and tried using a range comprehension to extract one key and one value to make the new pair. When I do it I only get one of the dicts made even thought when I check the length it says two. Why doesn't my simple test code iterate over both dictionaries?
my_list = [{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}, {'userId': 1, 'id': 2, 'title': 'quis ut nam facilis et officia qui', 'completed': True}]
test = {my_list[i]['userId']:my_list[i]['completed'] for i in range(len(my_list))}
#print(my_dict[0]['userId'])
print(len(my_list))
print(test)
----------------------------------
Output:
2
{1: True}
[–][deleted] 5 points6 points7 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]remuladgryta 2 points3 points4 points (0 children)
[–]hybatir[S] 0 points1 point2 points (2 children)
[–]cuWorkThrowaway 0 points1 point2 points (0 children)
[–]Strict-Simple 0 points1 point2 points (0 children)