-1

I was excepting all the elements to print but it prints only HFFDFD and fjdkl. why does this happens. Here's my code:

print (set({'Hffdfd' : 'shfs', 'fjdkl' : 616}))
anon
  • 1,258
  • 10
  • 17
mohitmonu
  • 117
  • 1
  • 2
  • 7

2 Answers2

1

You want to use dict instead of set. Try this:

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

EDIT: In fact, that is already a dict so you can just do:

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

You are confusing set with dict. You can see a good explanation here.

And if you want to sequentially print all the values and not the as a dict, you can do this:

>>> dct = {'Hffdfd' : 'shfs', 'fjdkl' : 616}
>>> for x in dct:
>>>   print(x)
>>>   print(dct[x])
Hffdfd
shfs
fjdkl
616
anon
  • 1,258
  • 10
  • 17
0

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})
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555