0

What is the output of the following code?

nums = {2 : 'two', 1: 'three', 3:'four'} for each in set(nums.keys()): if nums[each] > 'four': print(each) I'm not sure if the output will be 21 or 12 because I'm unaware of the order of keys in the set that's iterated on.

10th Jun 2020, 5:27 PM
Vivacious
4 Respuestas
+ 4
You can directly iterate on the dict.keys without using a set. In the code set is used to avoid duplicates? The keys are also unique , so you can work without using a set. But what you are comparing with: if nums[each] > 'four'? So you compare for example 'two' > 'four'. You need to explain this 😉😃 nums = {2 : 'two', 1: 'three', 3:'four'} for each in nums.keys(): if nums[each] > 'four': print(each)
10th Jun 2020, 6:01 PM
Lothar
Lothar - avatar
+ 4
RAJESH SAHU, a set does not necessarily puts the numbers in sorted order. If you try this: print(set([66,120,15,997,334,1024])) the result will be: {1024, 66, 997, 334, 15, 120}
10th Jun 2020, 8:07 PM
Lothar
Lothar - avatar
+ 1
Lothar, thank you for the response on an alternate way of writing the code. But please look at it as a question on the output and not on the implementation.
11th Jun 2020, 9:56 AM
Vivacious
0
Answer will be: 1 2 As set arrange the values in the ascending order
10th Jun 2020, 6:48 PM
ANJALI SAHU