0

I am a beginner in python and I am using python 2.4.3.

I have a question regarding to the order resulted from the set()function.

I understand set() will remove the the duplicate elements from a string and

[class set([iterable])

Return a new set object, optionally with elements taken from iterable.]1

But for example, when I do the following

a='abcdabcd' 
set(a)

it returned a result of

set(['a','c','b','d'])

in stead of

set(['a','b','c','d'])

which I would actually expect.

Why is that? I am not able to understand how the output was generated.

Many thanks in advance.

Community
  • 1
  • 1
Meruemu
  • 611
  • 1
  • 8
  • 28

1 Answers1

0

A set is defined as an "unordered collection of unique elements" (see here). The set object in Python make no guarantees about ordering, and you should not expect nor rely on the order to stay the same.

Erik Godard
  • 5,930
  • 6
  • 30
  • 33
  • 1
    I think that this is _mostly_ correct, but it does make one (sometimes useful) guarantee about the order -- specifically, multiple iterations over the same set will yield items in the same order provided that you haven't added or removed any items. – mgilson Jul 14 '16 at 23:02