mohitmonu mohitmonu - 3 years ago 117
Python Question

Doubts in python code how to use set method

i have excepted all the elements to print but it prints only HFFDFD and fjdkl why this happens

print (set({'Hffdfd' : 'shfs', 'fjdkl' : 616}))

Answer Source

Because if you iterate over a dictionary, you only obtain the keys. Next you put all these keys into a set that you print.

You can however use the following set comprehension to print both keys and values:

d = {'Hffdfd' : 'shfs', 'fjdkl' : 616}
print(set(y for x in d.items() for y in x))

Now you will construct a set that contains both keys and values.

If you however wish to print the dictionary itself, you can simply use:

print ({'Hffdfd' : 'shfs', 'fjdkl' : 616})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download