Home > Backend Development > Python Tutorial > Why Do Python Sets Appear to Have an Order?

Why Do Python Sets Appear to Have an Order?

Barbara Streisand
Release: 2024-11-14 22:27:02
Original
640 people have browsed it

Why Do Python Sets Appear to Have an Order?

Understanding the Apparent Order in Python Sets

While Python sets are inherently unordered, the seemingly consistent display order has raised questions. This article explores the mechanism behind this apparent ordering.

Hashing and Memory Placement

Python uses hashing to determine the memory placement for elements in a set. Each element's hash is computed, and the last N bits (determined by the set's size) are used as array indices. This explains why elements with certain hash values end up in specific memory locations.

Collision Resolution

When hash collisions occur (when two elements have the same hash), a collision resolution algorithm steps in. This algorithm determines which elements occupy the "best" memory locations. The resolution process partially determines the ordering of elements within the set.

Insertion Order (Python 3.6 and above)

Starting with CPython 3.6, dictionaries (not sets) gained the ability to preserve insertion order for iteration. This feature allows elements to be iterated in the same order they were inserted, even if their hashes conflict. However, this insertion order preservation is not currently extended to sets.

Examples

Consider the following examples:

set_1 = set([5, 2, 7, 2, 1, 88])
set_2 = set([5, 2, 7, 2, 1, 88])
print(set_1)  # prints set([88, 1, 2, 5, 7])
print(set_2)  # prints set([88, 1, 2, 5, 7])
Copy after login

In this case, the consistent ordering is due to identical hash values for all elements. Hash collisions result in the same collision resolution, leading to identical ordering.

However, this ordering is not guaranteed and can change if elements are inserted in a different order or if hashes change. For example:

list1 = [8, 16, 24]
set(list1)        # prints set([8, 16, 24])
list2 = [24, 16, 8]
set(list2)        # prints set([24, 16, 8])
Copy after login

The reverse ordering in the second set is a result of different collision resolution outcomes due to reordering the input list.

Conclusion

The apparent order in Python sets arises from a combination of hashing, memory placement, and collision resolution. While insertion order is not guaranteed, it can appear in some scenarios due to hash consistency and collision resolution outcomes.

The above is the detailed content of Why Do Python Sets Appear to Have an Order?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template