Why the result doesn't give {8,9,7}, Set always give the values in their own order... | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

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

15th Feb 2024, 1:22 PM
Theo Martier
Theo Martier - avatar
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)
15th Feb 2024, 2:00 PM
𝘕𝘉
𝘕𝘉 - avatar
+ 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]
15th Feb 2024, 10:26 PM
Rain
Rain - avatar
+ 1
Thank you , NB and Rain 👍
16th Feb 2024, 11:16 AM
Theo Martier
Theo Martier - avatar