Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:04:33+00:00 2026-05-24T20:04:33+00:00

I was under the impression that set() would order a collection much like .sort()

  • 0

I was under the impression that set() would order a collection much like .sort()

However it seems that it doesn’t, what was peculiar to me was why it reorders the collection.

>>> h = '321'
>>> set(h)
set(['1', '3', '2'])
>>> h
'321'
>>> h = '22311'
>>> set(h)
set(['1', '3', '2'])

why doesn’t it return set([‘1’, ‘2’, ‘3’]). I also seems that no matter how many instances of each number I user or in what order I use them it always return set([‘1’, ‘3’, ‘2’]). Why?

Edit:

So I have read your answers and my counter to that is this.

>>> l = [1,2,3,3]
>>> set(l)
set([1, 2, 3])
>>> l = [3,3,2,3,1,1,3,2,3]
>>> set(l)
set([1, 2, 3])

Why does it order numbers and not strings?

Also

import random
l = []
for itr in xrange(101):
    l.append(random.randint(1,101))

print set(l)

Outputs

>>> 
set([1, 2, 4, 5, 6, 8, 10, 11, 12, 14, 15, 16, 18, 19, 23, 24, 25, 26, 29, 30, 31, 32, 34, 40, 43, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 69, 70, 74, 75, 77, 79, 80, 83, 84, 85, 87, 88, 89, 90, 93, 94, 96, 97, 99, 101])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:04:34+00:00Added an answer on May 24, 2026 at 8:04 pm

    python set is unordered, hence there is no guarantee that the elements would be ordered in the same way as you specify them

    If you want a sorted output, then call sorted:

    sorted(set(h))
    

    Responding to your edit: it comes down to the implementation of set. In CPython, it boils down to two things:

    1) the set will be sorted by hash (the __hash__ function) modulo a limit

    2) the limit is generally the next largest power of 2

    So let’s look at the int case:

    x=1
    type(x) # int
    x.__hash__() # 1
    

    for ints, the hash equals the original value:

    [x==x.__hash__() for x in xrange(1000)].count(False) # = 0
    

    Hence, when all the values are ints, it will use the integer hash value and everything works smoothly.

    for the string representations, the hashes dont work the same way:

    x='1'
    type(x)  
    # str
    x.__hash__()
    # 6272018864
    

    To understand why the sort breaks for [‘1′,’2′,’3’], look at those hash values:

    [str(x).__hash__() for x in xrange(1,4)]
    # [6272018864, 6400019251, 6528019634]
    

    In our example, the mod value is 4 (3 elts, 2^1 = 2, 2^2 = 4) so

    [str(x).__hash__()%4 for x in xrange(1,4)]
    # [0, 3, 2]
    [(str(x).__hash__()%4,str(x)) for x in xrange(1,4)]
    # [(0, '1'), (3, '2'), (2, '3')]
    

    Now if you sort this beast, you get the ordering that you see in set:

    [y[1] for y in sorted([(str(x).__hash__()%4,str(x)) for x in xrange(1,4)])]
    # ['1', '3', '2']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was under the impression that with key-value coding, I could set any undefined
I was under the impression that I could run it on any machine, but
I'm under the impression that the Dot '.' (wild card) character is dangerous to
I was under the impression that using the Resolve method returned a new instance
I was under the impression that I could put any old executable program in
I was under the impression that when you double click a file (or choose
I was under the impression that if I deployed my application (using Ant) then
I was under the impression that this was formed correctly, but here it is
I was under the impression that jQuery is JavaScript Framework, but when am searching
I was under the impression that this code #include <windows.h> #include <stdio.h> int WINAPI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.