[Tutor] problem with using set

Dave Angel d at davea.name
Thu Oct 13 18:43:54 CEST 2011


On 10/13/2011 10:44 AM, Praveen Singh wrote:
> <SNIP>
>
> 2.
>>>> a='microsoft'
>>>> set(a)
> set(['c', 'f', 'i', 'm', 'o', 's', 'r', 't'])
>>>> print ''.join(set(a))
> cfimosrt
>
> When i print "Hello", i get the output as "helo"(in same sequence) but when
> i write "microsoft" i get this-"cfimosrt". So, it means i can't predict the
> outcome of set(a)??
>
The set() function converts its input into a set.  A set is not ordered 
in the usual way, but in such a way as to rapidly find whether a 
particular item exists already in the set, and insert it if not.  
Inserting a new item might totally rearrange the existing ones, set 
doesn't promise anything at all about the order.  (Dictionaries are 
similar, but while set has only a key, dict has both key and value)

A set has no value to the assignment as stated.  Stick to lists.


-- 

DaveA



More information about the Tutor mailing list