Python Forum
How does a set in python store the elements?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does a set in python store the elements?
#1
I am coming learning python after working with c++ for a few years. In c++ when you insert into a set it orders the elements, I can't figure out why in python sometimes the set will order the elements and sometimes it does not. Here are a few examples:

a = set([3, 2, 1])
print(a)
prints: {1, 2, 3}

a = set([1, 2, 3])
print(a)
prints: {1, 2, 3}

a = set([5, 7, 9])
print(a)
prints: {9, 5, 7}

a = set([7, 9, 5])
print(a)
prints: {9, 5, 7}

a = set([5, 3, 7])
print(a)
prints: {3, 5, 7}

I don't know what's going on here, I know you can sort the set using sorted() but the way it originally stores the information doesn't make any sense to me.
Reply
#2
That's interesting. Test that I've tried:
using 3 digits return is in order if you do not use a digit higher than 7.
using 5 digits it returns correct order.
a = set([4,2,7])
print(a)
Output:
{2, 4, 7}
a = set([4,6,8,10,7])
print(a)
Output:
{4, 6, 7, 8, 10}
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior
https://docs.python.org/3.8/library/stdt...-frozenset
Reply
#4
It hashes the elements, then uses the last N bits of the hash, where N depends on the size of the set. For the small set given, N is probably 3.

>>> format(hash(9) & 0x7, "b")
'1'
>>> format(hash(5) & 0x7, "b")
'101'
>>> format(hash(7) & 0x7, "b")
'111'
So the hash of 9 is smallest, 5, is next, then 7.

If the hash is identical, then order of insertion matters.

7 and 15 have the same hash value (in small sets).

>>> print(set((7, 15)))
{15, 7}
>>> print(set((15, 7)))
{7, 15}
Reply
#5
There where discussion also about sets order,when in Python 3.7 modern dict where implement that are guarantee to keep order.
R. Hettinger Wrote:In short, the implementation of modern dicts that preserves insertion order is unique and not considered appropriate with sets.
In particular,dicts are used everywhere to run Python (e.g. __dict__ in namespaces of objects).
A major motivation behind the modern dict was to reduce size, making Python more memory-efficient overall.
In contrast,sets are less prevalent than dicts within Python's core and thus dissuade such a refactoring

Having unordered sets are consistent with a very generic and ubiquitous data structure that unpins most modern math, i.e. Set Theory.
I submit,unordered sets in Python are good to have.
Reply
#6
For a set that preserve the insertion order, you could try sortedcollections.OrderedSet from pypi.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 986 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,550 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Reading blob data from database by python and store it in .zip format Adityasi 2 6,445 Nov-18-2019, 05:22 PM
Last Post: ibreeden
  How do I install apps from google play store? using python + selenium.. mongo 0 2,244 Aug-05-2019, 12:41 AM
Last Post: mongo
  Store a python list of lists in a database to use for searches later on klllmmm 3 3,010 Jun-20-2019, 07:54 AM
Last Post: buran
  Receive Serial Data and store in different Variables in Python jenkins43 5 5,489 Dec-28-2018, 01:33 PM
Last Post: snippsat
  Rearranging elements in Python Nirmal 10 5,021 Nov-23-2018, 12:05 PM
Last Post: Nirmal
  How to sum up the elements of an integer using python split/join? mohanraj1986 5 3,462 Aug-27-2018, 09:13 AM
Last Post: perfringo
  Python simple store inventory system. 2skywalkers 3 9,758 Aug-19-2018, 06:12 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020