← Back to Python Course | Chapter 6: Data Structures | Lesson 4 of 12

Python Sets

Set चीज़ों का एक बैग है जिसमें हर item अलग होता है और कोई क्रम नहीं होता, जैसे unique stickers का collection। कोई duplicate जोड़ने पर कुछ नहीं होता।
Syntax
python
set_name = {item1, item2, item3}
set_name = set(iterable)

Set क्या है?

Set unique, unordered elements store करता है — कोई duplicate जोड़ने पर चुपचाप कुछ नहीं होता — और चूँकि sets अंदर hashing इस्तेमाल करते हैं, membership checks किसी list को scan करने से कहीं तेज़ होते हैं।

Curly braces {} एक set define करते हैं, पर एक खाली {} असल में dict बनाता है, इसलिए खाली set के लिए set() इस्तेमाल कीजिए।

उदाहरण: What is a Set?

python
numbers = {1, 2, 2, 3}  # duplicate 2 is silently dropped
print(numbers)
empty = set()  # {} alone would create a dict, not a set
print(type(empty))

Items जोड़ना और हटाना

add(item) एक नया single element डालता है (पहले से मौजूद होने पर कोई असर नहीं), remove(item) उसे हटाता है पर न मिलने पर KeyError raise करता है, और discard(item) वही removal बिना कोई error raise किए करता है — जब आप निश्चित नहीं हों कि item मौजूद है, तो discard() ज़्यादा सुरक्षित choice है।

उदाहरण: Adding and Removing Items

python
s = {1, 2, 3}
s.add(4)  # inserts a new element
s.discard(10)  # no error even though 10 isn't in the set
print(s)

Union और Intersection

union() (या | operator) दोनों sets के हर element वाला एक नया set लौटाता है और duplicates अपने-आप हट जाते हैं, जो set-theory का वह equivalent है जिसमें दो guest lists को बिना repeat के एक में मिलाया जाता है।

उदाहरण: Union and Intersection

python
a = {1, 2, 3}
b = {2, 3, 4}
print(a.union(b))  # every element from both sets, no duplicates
print(a.intersection(b))  # only elements present in both

Set Difference

difference() (या -) पहले set के वे elements लौटाता है जो दूसरे में नहीं हैं, यह जानने के लिए उपयोगी है कि 'A में क्या है जो B में missing है'; symmetric_difference() (या ^) वे elements लौटाता है जो दोनों में से किसी एक set में हैं, दोनों में नहीं।

उदाहरण: Set Difference

python
a = {1, 2, 3}
b = {2, 3, 4}
print(a.difference(b))  # in a but not in b
print(a.symmetric_difference(b))  # in exactly one of the two sets

Set Membership और Checking

in membership उसी तरह check करता है जैसे lists के लिए करता है, पर set पर यह size चाहे जो हो, लगभग constant time में चलता है; issubset() जाँचता है कि क्या एक set का हर element दूसरे set में भी मौजूद है, जो permission या capability checks के लिए काम आता है।

उदाहरण: Set Membership and Checking

python
a = {1, 2}
b = {1, 2, 3}
print(1 in b)  # membership check, fast on a set
print(a.issubset(b))  # True since every element of a is also in b
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.