Currently I am doing it like this..
def operation(x):
return x
items_seen = []
d = {"a":10, "b": 5,"c": 6,"d": 7, "e": 7}
for x in d.values():
if x not in items_seen:
print operation(x)
items_seen.append(x)
But I was wondering if there was a better way.. ?
You can convert your values list
to a set
first to ensure that every value only occurs once:
for x in set(aList.values()):
print operation(x)
If you only want to apply the operation
function to the unique values of a dictionary, you can iterate over the set
of its values:
def operation(x):
return x
d = {"a": 10, "b": 5, "c": 6, "d": 7, "e": 7}
for x in set(d.values()):
print operation(x)
Output
10
5
6
7
Aside: I've changed your dictionary name from aList
to d
for clarity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.