Why Python Sets Treat 1, 1.0, and True as the Same Element – The Hidden Pitfall Explained
This article explores a surprising behavior of Python sets where values like 1, 1.0, True, and even complex numbers are considered identical, explains the underlying use of the 'is' and '==' operators, and demonstrates how this affects deduplication with clear code examples.
Introduction
In a recent Python community discussion, a user asked about an unexpected result when working with Python sets. The author shares the question and provides a detailed explanation.
Problem Statement
The confusion arises from how Python compares objects using is (identity, comparing memory addresses) versus == (value equality). This distinction is crucial when dealing with sets, which automatically remove duplicate elements.
Solution Process
Python sets deduplicate based on value equality ( ==) for immutable types such as int, float, bool, and complex. Consequently, expressions like True == 1 evaluate to True, and the set {1, 1.0, 1.00, True} collapses to {1}.
Example: print({1, 1.0, 1.00, True}) # Output: {1} The article also references a previous tutorial where boolean values were used in string slicing, linking to additional resources.
Data Type Overview
Python’s basic data types are divided into immutable and mutable categories:
Immutable: Number (int, float, bool, complex), String , Tuple
Mutable: List , Dictionary , Set
All numeric types belong to the Number category and are compared using ==. Therefore, True == 1 == 1.0 == 1e0 evaluates to True.
Practical Example with pandas
A pandas snippet demonstrates extracting region information based on specific province names:
df['地区2'] = df.地区.apply(lambda s: s[:(s in ("黑龙江省", "内蒙古自治区"))] + 2])This logic showcases how Python’s comparison operators can be leveraged in data processing.
Conclusion
The article summarizes the set “gotcha”, reinforces understanding of Python’s data type comparisons, and thanks contributors who helped clarify the concepts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
