I'm struggling to understand a script from "the Python Book". The script removes duplicates from a list, by putting every element into a dict as a key (for example with 'none' as value) and then returns the dict.keys(). The script is as follows:
from operator import setitem
def distinct(l):
d = {}
map(setitem, (d,)*len(l), l, [])
return d.keys()
I have run the script, passing a list which contains duplicates to distinct():
li = [1, 5, 6, 1, 8]
x = distinct(li)
print(x)
However when I run the script and print the result I just get:
dict_keys([])
Can somebody give me an example of how I can run the distinct() function so I can get some meaningful output? Also please can someone explain how the map function works with the inner setitem function. I have read about setitem, but I'm not sure how it is working here. I'm confused by the first the argument to setitem:
(d,)*len(l)