Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
Why the result doesn't give {8,9,7}, Set always give the values in their own order...
https://sololearn.com/compiler-playground/c03kCSDRtRdr/?ref=app
3 Antworten
+ 3
Hi Theo Martier
Sets stores unique values and the insertion order is not guaranteed to be maintained.
I think you can convert both the sets to list type like below to get required output
firstList=list(first)
secondList=list(second)
finalList=[x for x in secondList if x not in firstList]
print(finalList)
+ 2
"""
Theo Martier ,
The official docs on set:
https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset
You can also do it this way.
"""
first = {1, 2, 3, 4, 5, 6}
second = {4, 5, 6, 7, 8, 9}
print(sorted(list(second - first))) # [7, 8, 9]
+ 1
Thank you , NB and Rain 👍