0

I'm working with sets and notice when I do:

s1 = set( ['a', 'b', 'c'])
s2 = set([1,2,3])

print s1.symmetric_difference(s2)

It prints it off as:

set(['a', 1, 2, 3, 'c', 'b'])

Is there any reason to why it displays it in the order of 'a', 1, 2, 3, 'c', 'b' and not simply 'a', 'b', 'c', 1, 2, 3? Or because sets are unordered it doesn't matter how it's printed and both would be the same?

Thanks!

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by pyTony
0

It is because of unorderedness, and the order in sets varies:

>>> s1 & s2
set([])
>>> s1 | s2
set(['a', 1, 'c', 'b', 3, 2])
>>> s1.symmetric_difference(s2)
set(['a', 1, 2, 3, 'c', 'b'])
>>>

Edited by pyTony: n/a

0

I see that both s1 | s2 and s1.symmetric_difference(s2) result in sets with the same values

>>> s1 | s2
set(['a', 1, 'c', 'b', 3, 2])
>>> s1.symmetric_difference(s2)
set(['a', 1, 2, 3, 'c', 'b'])

If asked, would saying set() be a correct answer for s1 | s2 and s1.symmetric_difference(s2) or does order matter then? Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.