Python                                                          Home : www.sharetechnote.com

 

 

 

 

Python - Set

 

Set in Python is an unordered collection of distict elements. This is a data structure which is like a listbox or dropdown box you see in GUI program.

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

  • Creating a Set : Direct Creation - Example 1
  • Creating a Set : Using set() function - Example 2
  • Creating a Set : Creating from a String - Example 3
  • Accessing a Set : Getting one item - Example 4
  • Getting the length (size) of a List - Example 5
  • Accessing a Set : Looping through every elements - Example 6
  • Adding an element : Adding an element to a set - Example 7
  • Removing an element : Removing an element from a set - Example 8
  • Set Operation : Intersection - Example 9
  • Set Operation : difference - Example 10
  • Set Operation : union - Example 11

 

 

Examples :

 

< Example 1 >

 

fruits = {"Apple","BlueBerry","Banana","SrawBerry"}

print(fruits)

 

Result :----------------------------------

 

{'SrawBerry', 'Apple', 'BlueBerry', 'Banana'}

 

 

 

< Example 2 >

 

fruits = set(["Apple","BlueBerry","Banana","SrawBerry"])

print(fruits)

 

Result :----------------------------------

 

{'SrawBerry', 'Apple', 'BlueBerry', 'Banana'}

 

 

< Example 3 >

 

words = set("This is a example of creating a Set".split())

print(words)

 

Result :----------------------------------

{'creating', 'This', 'a', 'of', 'Set', 'example', 'is'}

 

 

< Example 4 >

 

fruits = set(["Apple","BlueBerry","Banana","SrawBerry"])

 

// Set does not support direct 'indexing', so you need to convert it to a list

// for direct indexing

print(list(fruits)[2])

 

Result :----------------------------------

 

SrawBerry

 

 

< Example 5 >

 

fruits = {"Apple","BlueBerry","Banana","SrawBerry"}

print(len(fruits))

 

Result :----------------------------------

 

4

 

 

< Example 6 >

 

fruits = {"Apple","BlueBerry","Banana","SrawBerry"}

 

for fruit in fruits :

    print(fruit, ' ', end ='')

 

Result :----------------------------------

 

BlueBerry  Banana  SrawBerry  Apple

 

 

< Example 7 >

 

fruits = {"Apple","BlueBerry","Banana","SrawBerry"}

fruits.add("Grape")

print(fruits)

 

Result :----------------------------------

 

{'BlueBerry', 'Banana', 'Grape', 'SrawBerry', 'Apple'}

 

 

< Example 8 >

 

fruits = {"Apple","BlueBerry","Banana","SrawBerry"}

fruits.remove("Apple")

print(fruits)

 

Result :----------------------------------

 

{'BlueBerry', 'Banana', 'SrawBerry'}

 

 

< Example 9 >

 

A = {"Apple","BlueBerry","Banana","SrawBerry"}

B = {"Mango","Pineapple","Banana"}

 

print(A.intersection(B))

print(B.intersection(A))

 

Result :----------------------------------

 

{'Banana'}

{'Banana'}

 

 

< Example 10 >

 

A = {"Apple","BlueBerry","Banana","SrawBerry"}

B = {"Mango","Pineapple","Banana"}

 

print(A.difference(B))

print(B.difference(A))

 

Result :----------------------------------

 

{'SrawBerry', 'Apple', 'BlueBerry'}

{'Pineapple', 'Mango'}

 

 

< Example 11 >

 

A = {"Apple","BlueBerry","Banana","SrawBerry"}

B = {"Mango","Pineapple","Banana"}

 

print(A.union(B))

print(B.union(A))

 

Result :------------------------------------

 

{'Apple', 'BlueBerry', 'SrawBerry', 'Pineapple', 'Mango', 'Banana'}

{'Pineapple', 'Apple', 'BlueBerry', 'SrawBerry', 'Mango', 'Banana'}

 

 

< Example 16 >