Reputation: 1
I have a dictionary created like:
di = {
"R": [{7, 9}, {5, 8}],
"N": [{6, 9}, {8, 8}],
"L": [{7, 9}, {5, 0}],
"P": [{0, 9}, {7, 8}]
}
I want to square dic["R"]
numbers so the end result would be {49, 81}, {25, 64}
.
So far I have:
for i in dic['R']:
squared = [j ** 2 for j in i]
but it only returns the second list.
Upvotes: 0
Views: 163
Reputation: 3066
The problem is that squared gets overwritten. You can use this if you want a list as result:
squared = []
for i in dic['R']:
squared.append([j ** 2 for j in i])
Upvotes: 1
Reputation: 71454
Note that these are not lists of ints, or lists of lists of ints, they are lists of sets of ints:
>>> di = {
... "R": [{7, 9}, {5, 8}],
... "N": [{6, 9}, {8, 8}],
... "L": [{7, 9}, {5, 0}],
... "P": [{0, 9}, {7, 8}]
... }
>>> di["R"] = [{i ** 2 for i in s} for s in di["R"]]
>>> di
{'R': [{81, 49}, {64, 25}], 'N': [{9, 6}, {8}], 'L': [{9, 7}, {0, 5}], 'P': [{0, 9}, {8, 7}]}
Upvotes: 2