Python Set Add Python Set Difference Python Set Environment Variable Python Set Function Python Set Intersection Python Set Methods Python Set to List Python Set Union Python Set Working Directory

15 Practical Python Set Examples with a Sample Program

Units are principally collection of sure gadgets which are unordered. There isn’t any specific order during which they are stored. In Python units are similar, however there are few differences with primary sets.

  • The elements in python units are unique, there can’t be duplicate gadgets in python units. If duplicate gadgets entered, it is going to be ignored and remaining set will all the time include unique parts.
  • Python sets are mutable. However, its parts are immutable. As soon as entered gadgets cannot be modified.
  • Python set’s item cannot be accessed utilizing indexes. There isn’t any index hooked up to set gadgets.

This tutorial explains the next examples in Python set:

  1. Create a Simple Set
  2. Create a Set with Constructor
  3. Create an Empty Set
  4. Create a Set from Listing
  5. Create a Set from Tuple
  6. Access a Specific Worth in Set
  7. Add Gadgets to a Set – Using Add Technique
  8. Add Gadgets to a Set – Using Replace Technique
  9. Take away Gadgets from Set – Utilizing Discard Technique
  10. Remove Gadgets from Set – Utilizing Take away Technique
  11. Union Operation on Sets
  12. Intersection Operation on Units
  13. Difference Operation on Units
  14. Symmetric Difference of Two Sets
  15. Examine two Sets
  16. Membership Check on Sets
  17. Iterating on Set
  18. Clearing a Set
  19. Fascinating Drawback Solved using Sets
  20. Full Instance Code of Units – sets.py

1. Create a Simple Set

Python sets may be created by merely writing the gadgets within curly braces.

>>> primes=3,5,7,9,11,13

>>> sort(primes)

Observe: Remember that Python dictionaries additionally makes use of in declaration. But, within the above example, we’ve created a set as indicated by the sort command output.

For Python dictionaries, check with this: Python Dictionary Examples – Create, Replace and Delete Parts

2. Create a Set with Constructor

Use set as shown under and move the initial string as constructor.

In the following instance, we are purposefully including one other “s” in entrance of “stuff”, which will probably be ignored by the set. It’s going to take just one “s” even once we gave multiple.

>>> unique=set(“thegeeksstuff”);

>>> unique
‘h’, ‘t’, ‘u’, ‘s’, ‘f’, ‘e’, ‘g’, ‘okay’

>>> sort(distinctive)

3. Create an Empty Set

The following is an incorrect method of making an empty set. It will create an empty dictionary:

>>> empty=

>>> sort(empty)

The right method to create is proven under. To create an empty set, use the set technique with empty constructor as proven under.

>>> empty=set();

>>> sort(empty)

Whereas making an attempt to create empty set, use constructor technique. In any other case utilizing 1st technique will make an empty dictionary as an alternative.

four. Create a Set from Listing

First create a listing as shown under:

>>> primesList=[3,5,7,11,13,17];

To study extra about record, confer with this: Python Listing Examples – Insert, Append, Length, Index, Take away, Pop

Subsequent, create a set using the above listing:

>>> primeSet=set(primesList)

>>> sort(primeSet)

>>> primeSet
three, 5, 7, 11, 13, 17

5. Create Set from Tuple

First, create a tuple as shown under.

>>> p=(3,5,7,11,13);

>>> p
(three, 5, 7, 11, 13)

>>> sort(p)

To study more about tuples, discuss with this: 17 Practical Python Tuples Examples

Subsequent, create a set from the above tuple:

>>> primes=set(p);

>>> p
(three, 5, 7, 11, 13)

>>> sort(primes)

>>> primes
3, 5, 7, 11, 13

6. Access a Specific Worth in Set

Set parts can’t be accessed specifically, there isn’t a index hooked up to it. We can’t entry parts of set.

>>> primes = three, 5, 7, 11, 13

>>> primes[1] Traceback (most recent call last):
File ““, line 1, in
primes[1] TypeError: ‘set’ object does not help indexing

7. Add Gadgets to a Set – Utilizing Add Technique

Gadgets may be added to set utilizing two methods add and replace. Add technique can insert single factor but update technique can insert tuple, lists too.

>>> primes=three,5,7,11;

>>> primes
11, three, 5, 7

>>> primes.add(19);

>>> primes
3, 5, 7, 11, 19

eight. Add Gadgets to a Set – Using Replace Technique

The next will add the gadgets from the plist to the primes set.

>>> plist=[12,14,15,16];

>>> primes.replace(plist);

>>> primes
3, 5, 7, 11, 12, 14, 15, 16, 19

9. Take away Gadgets from Set – Using Discard Technique

Gadgets may be removed from set using discard and remove technique. While removing gadgets, if merchandise does not exist, take away reviews and error while discard doesn’t.

>>> primes
three, 5, ‘santosh’, 7, 11, 12, 14, 15, 16, 19

>>> primes.discard(“santosh”)
>>> primes
3, 5, 7, 11, 12, 14, 15, 16, 19

>>> primes.discard(“santosh”);

Whereas making an attempt to remove already removed item, discard doesn’t report an error.

10. Take away Gadgets from Set – Using Remove Technique

At present primes set has the next values.

>>> primes
three, 5, 7, 11, 12, 14, 15, 16, 19

The next will take away the item with value 11 from the primes set.

>>> primes.remove(11);

As you see under, we don’t see item with worth 11 anymore in primes set.

>>> primes
3, 5, 7, 12, 14, 15, 16, 19

>>> primes.take away(11);
Traceback (most up-to-date call last):
File ““, line 1, in
primes.take away(11);
KeyError: 11

Remove technique reported error when making an attempt to remove aspect which doesn’t exist.

11. Union Operation on Sets

Union operation (|) will create a new set with distinct gadgets from each the units.

>>> prime1=3,5,7,11;

>>> prime2=3,5,7,11,13,17,19

>>> primes=prime1 | prime2

>>> primes
3, 5, 7, 11, 13, 17, 19

12. Intersection Operation on Sets

Intersection operation (&) will create a new set with gadgets widespread to each the units.

>>> prime1=three,5,7,11;

>>> prime2=3,5,7,11,13,17,19

>>> primes=prime1 & prime2

>>> primes
3, 11, 5, 7

13. Difference Operation on Sets

Difference operation will create a new set containing parts which are in first set however not in the second set

>>> prime2=3,5,7,11,13,17,19

>>> prime1=3,5,7,11;

>>> primes=prime2-prime1

>>> primes
17, 19, 13

14. Symmetric Distinction of Two Units

Symmetric difference of two units will create a new set containing parts from each the sets except widespread parts.

>>> prime2=3,5,7,11,13,17,19

>>> prime1=3,5,7,11,91,101;

>>> primes=prime2 ^ prime1

>>> primes
17, 19, 101, 91, 13

15. Examine two Units

Utilizing comparability we will verify if one set is superset or subset or equal to another set.

>>> prime1=three,5,7,11,91,101;

>>> prime=3,5,7,11,91,101;

>>> check=prime==prime1

>>> check
True

>>> prime1=three,5,7,11,91,101;

>>> prime2=three,5,7,11,13,17,19

>>> check=prime2>> check
False

>>> prime2=three,5,7,11,13,17,19

>>> prime1=three,5,7,11,91,101;

>>> prime1=three,5,7,11;

>>> check=prime2>> check
False

>>> check=prime2>prime1

>>> check
True

16. Membership Check on Sets

We will check if an element exist or not in specified set.

>>> prime1=three,5,7,11,91,101;

>>> check= three in prime1

>>> check
True

>>> check=13 in prime1

>>> check
False

17. Iterating on Set

Let us use the following primes set for this example.

>>> primes=three,5,7,11,91,101;

use for loop to loop via the set parts.
>>> for prime in primes:
print(prime);
three
5
101
7
11

To study extra about for loop, discuss with this: 12 Essential Python For Loop Command Examples

18. Clearing a Set

Using clear technique, all the gadgets of set might be deleted.

>>> prime1
3, 5, 101, 7, 11, 91

>>> prime1.clear()

>>> prime1
set()

19. Fascinating Drawback Solved utilizing Units

Drawback statement: given a record of integers, print all of the distinct parts.
Utilizing units we will clear up this drawback in single line of code.

>>> number = [1,2,3,4,5,2,3,4,7,8,9,8,12,13,14,14,19]

>>> quantity
[1, 2, 3, 4, 5, 2, 3, 4, 7, 8, 9, 8, 12, 13, 14, 14, 19]

>>> print (set(quantity))
1, 2, three, four, 5, 7, eight, 9, 12, 13, 14, 19

This was a use case, equally sets may be helpful in other situations.

20. Full Example Code of Sets – units.py

Create a units.py file

vi sets.py

Copy/paste the following into sets.py and execute it on your testing.

#1) Creating set
primes=three,5,7,9,11,13
sort(primes)

# 2) Creating set with constructor
distinctive=set(“thegeeksstuff”);
distinctive

# three) Creating empty set
empty=
sort(empty)
empty=set();
sort(empty)

#4) Creating set from Record
primesList=[3,5,7,11,13,17];
primeSet=set(primesList)
sort(primeSet)
primeSet

# 5) Creating set from tuple
p=(three,5,7,11,13);
primes=set(p);
sort(primes)
primes

# 6) Accessing values in Set
Primes = 3, 5, 7, 11, 13;
#Primes[1]; # uncommenting this throw error

#7) Using Add technique
primes=three,5,7,11;
primes.add(19);
primes

# eight) Utilizing replace technique
plist=[12,14,15,16];
primes.replace(plist);
primes

#9) Using discard
primes=3, 5, ‘santosh’, 7, 11, 12, 14, 15, 16, 19;
primes.discard(“santosh”)

# 10) Using remove
primes = three, 5, 7, 11, 12, 14, 15, 16, 19
primes.remove(11);
primes;

# 11) Union operation on units
prime1=3,5,7,11;
prime2=3,5,7,11,13,17,19
primes=prime1 | prime2
primes

# 12) Intersection operation on Sets
prime1=3,5,7,11;
prime2=3,5,7,11,13,17,19
primes=prime1 & prime2
primes

# 13) Distinction operation on units
prime2=3,5,7,11,13,17,19
prime1=3,5,7,11;
primes=prime2-prime1
primes

# 14) Symmetric distinction
prime2=three,5,7,11,13,17,19
prime1=three,5,7,11,91,101;
primes=prime2 ^ prime1
primes

# 15) Examine two sets
prime1=three,5,7,11,91,101;
prime=3,5,7,11,91,101;
check=prime==prime1
check

prime1=three,5,7,11,91,101;
prime1=3,5,7,11;
check=prime2Executing the above units.py will show the following output. Research the above code correctly to know why you’re getting the following output.

$ python units.py
3
5
7
11
91
101
set([1, 2, 3, 4, 5, 7, 8, 9, 12, 13, 14, 19])

In case you loved this article, you may also like..