Programming में कई बार हमें ऐसे Data की आवश्यकता होती है जिसमें Duplicate Values न हों। उदाहरण के लिए किसी कक्षा में उपस्थित विद्यार्थियों की सूची, Unique User IDs, Product Codes, Email IDs या किसी परीक्षा में शामिल विद्यार्थियों का Unique Record।
यदि ऐसे Data को List में रखा जाए, तो Duplicate Values आने की संभावना रहती है। इस समस्या का समाधान Set है।
Set Python का एक Built-in Collection Data Type है, जो केवल Unique Elements को Store करता है। Set में Duplicate Values अपने आप हट जाती हैं।
Set गणित (Mathematics) के Set Theory पर आधारित है, इसलिए इसमें Union, Intersection, Difference और Symmetric Difference जैसे Operations भी उपलब्ध हैं।
Set Python का एक Unordered Collection Data Type है, जिसमें केवल Unique Elements Store किए जाते हैं।
Set को Curly Braces {} के अंदर लिखा जाता है।
set_name = {item1, item2, item3}
fruits = {"Apple", "Mango", "Banana"}
print(fruits)
- Unordered Collection है।
- Duplicate Values स्वतः हट जाती हैं।
- Mutable होता है।
- Indexing उपलब्ध नहीं होती।
- Slicing उपलब्ध नहीं होती।
- Elements Hashable होने चाहिए।
- Mathematical Set Operations को Support करता है।
numbers = {10, 20, 30, 40}
numbers = set([10, 20, 30])
letters = set("Python")
print(letters)
❌ गलत
data = {}
यह Dictionary बनती है।
✔ सही
data = set()
numbers = {10,20,20,30,30,40}
print(numbers)
Output
{10,20,30,40}
data = {
10,
20.5,
"Python",
True
}
numbers = {10,20,30}
print(len(numbers))
numbers = {10,20,30}
print(20 in numbers)
print(50 not in numbers)
colors = {"Red","Blue","Green"}
for color in colors:
print(color)
Set में नए Elements जोड़े और हटाए जा सकते हैं, लेकिन किसी विशेष Index पर Element को बदला नहीं जा सकता क्योंकि Set में Indexing नहीं होती।
Python Set की सबसे महत्वपूर्ण विशेषता Mathematical Operations हैं।
दो Sets के सभी Unique Elements प्राप्त करने के लिए।
A = {1,2,3}
B = {3,4,5}
print(A | B)
Output
{1,2,3,4,5}
दो Sets के Common Elements प्राप्त करने के लिए।
A = {1,2,3}
B = {2,3,4}
print(A & B)
Output
{2,3}
पहले Set में मौजूद लेकिन दूसरे में नहीं।
A = {1,2,3}
B = {2,3,4}
print(A - B)
Output
{1}
जो Elements केवल किसी एक Set में हों।
A = {1,2,3}
B = {2,3,4}
print(A ^ B)
Output
{1,4}
A = {1,2}
B = {1,2,3,4}
print(A <= B)
print(B >= A)
A = {1,2}
B = {5,6}
print(A.isdisjoint(B))
यदि Set को Immutable बनाना हो, तो frozenset() का उपयोग किया जाता है।
numbers = frozenset([10,20,30])
print(numbers)
- Immutable होता है।
- नए Elements नहीं जोड़ सकते।
- हटा नहीं सकते।
- Dictionary Key के रूप में उपयोग किया जा सकता है।
|
Function |
उपयोग |
|
len() |
कुल Elements |
|
max() |
सबसे बड़ा |
|
min() |
सबसे छोटा |
|
sum() |
योग |
|
sorted() |
Sorting |
|
any() |
कोई True |
|
all() |
सभी True |
Python Set के प्रमुख Methods:
- add()
- update()
- remove()
- discard()
- pop()
- clear()
- copy()
- union()
- intersection()
- difference()
- symmetric_difference()
- issubset()
- issuperset()
- isdisjoint()
अगले अध्याय में इन सभी Methods को एक Master Guide में विस्तार से समझेंगे।
numbers = [10,20,10,30,20]
unique = set(numbers)
print(unique)
A = {1,2,3}
B = {3,4,5}
print(A.union(B))
A = {1,2,3}
B = {2,3,5}
print(A.intersection(B))
A = {1,2,3}
B = {2,3}
print(A.difference(B))
Program 5 – Symmetric Difference
A = {1,2,3}
B = {3,4,5}
print(A.symmetric_difference(B))
numbers = {[1,2],3}
❌ क्योंकि List Hashable नहीं होती।
- Duplicate Data हटाने के लिए Set का उपयोग करें।
- Membership Test के लिए Set सबसे तेज़ होता है।
- Mathematical Operations के लिए Set उपयुक्त है।
- Ordered Data के लिए List का उपयोग करें।
- Immutable Collection चाहिए तो frozenset() उपयोग करें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Set में Duplicate Values नहीं होती।
- Set Unordered Collection है।
- Set Mutable होता है।
- Indexing और Slicing उपलब्ध नहीं होती।
- frozenset() Immutable होता है।
- Union (|), Intersection (&), Difference (-), Symmetric Difference (^) महत्वपूर्ण Operators हैं।
Frequently Asked Questions (FAQs)
Set Python का ऐसा Collection Data Type है जिसमें केवल Unique Values Store होती हैं।
प्रश्न 2. क्या Set में Duplicate Values हो सकती हैं?
नहीं।
प्रश्न 3. क्या Set में Indexing होती है?
नहीं।
Immutable Set को Frozen Set कहते हैं।
प्रश्न 5. Set का सबसे बड़ा उपयोग क्या है?
Duplicate हटाने और Mathematical Set Operations करने में।
- Set = Unique Elements
- Duplicate = ✘
- Mutable = ✔
- Ordered = ✘
- Indexing = ✘
- Slicing = ✘
- frozenset() = Immutable Set
- | = Union
- & = Intersection
- - = Difference
- ^ = Symmetric Difference
Python Set Methods in Hindi – add(), update(), remove(), discard(), pop(), clear(), copy(), union(), intersection(), difference(), symmetric_difference(), issubset(), issuperset() और isdisjoint() सहित सभी Set Methods की Complete Guide।