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

  • Home
  • SEARCH
  • 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 7686585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:30:50+00:00 2026-05-31T19:30:50+00:00

Recently I noticed that when I am converting a list to set the order

  • 0

Recently I noticed that when I am converting a list to set the order of elements is changed and is sorted by character.

Consider this example:

x=[1,2,20,6,210]
print(x)
# [1, 2, 20, 6, 210] # the order is same as initial order

set(x)
# set([1, 2, 20, 210, 6]) # in the set(x) output order is sorted

My questions are –

  1. Why is this happening?
  2. How can I do set operations (especially set difference) without losing the initial order?
  • 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-31T19:30:52+00:00Added an answer on May 31, 2026 at 7:30 pm
    1. A set is an unordered data structure, so it does not preserve the insertion order.

    2. This depends on your requirements. If you have an normal list, and want to remove some set of elements while preserving the order of the list, you can do this with a list comprehension:

      >>> a = [1, 2, 20, 6, 210]
      >>> b = set([6, 20, 1])
      >>> [x for x in a if x not in b]
      [2, 210]
      

      If you need a data structure that supports both fast membership tests and preservation of insertion order, you can use the keys of a Python dictionary, which starting from Python 3.7 is guaranteed to preserve the insertion order:

      >>> a = dict.fromkeys([1, 2, 20, 6, 210])
      >>> b = dict.fromkeys([6, 20, 1])
      >>> dict.fromkeys(x for x in a if x not in b)
      {2: None, 210: None}
      

      b doesn’t really need to be ordered here – you could use a set as well. Note that a.keys() - b.keys() returns the set difference as a set, so it won’t preserve the insertion order.

      In older versions of Python, you can use collections.OrderedDict instead:

      >>> a = collections.OrderedDict.fromkeys([1, 2, 20, 6, 210])
      >>> b = collections.OrderedDict.fromkeys([6, 20, 1])
      >>> collections.OrderedDict.fromkeys(x for x in a if x not in b)
      OrderedDict([(2, None), (210, None)])
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've noticed that Lucene recently released v2.9 (on 25th September this year - 2009),
Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to
I've recently noticed that when I create private methods that set a few fields
Recently I noticed that my vs2010 was auto lower casing my html attributes. This
I recently noticed that colors are now displaying darker on iPhone than their actual
I have recently noticed that a lot of JavaScript files on the Web start
I have a few forms that use tiny mce. I have noticed recently that
I recently upgraded to using ReSharper 5 (currently in beta). I noticed that in
Recently I noticed my application appears to be eating memory that never gets released.
Something that I've noticed recently on two different machines is that Apache2 installed via

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.