# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# #  3.4 Set
#
# In Python, **Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements**.
#
# **Compare to a list, set has a highly optimized method for checking whether a specific element is contained in the set.**

# ## 3.4.1 Creating a Set
#
# Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’.
#
# **Note – A set cannot have mutable elements like a list or dictionary, as it is immutable.**

# +
# Creating an empty Set
set1 = set()
print("Initial blank Set: ")
print(set1)
 


# +
# Creating a Set with the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)

# Creating a Set with the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
# -

# - **A set contains only unique element, multiple duplicate values will be removed. Order of elements in a set is undefined and is unchangeable**. 
# - **Type of elements in a set can be different.** 

# +
# in the list we have duplicate 3, 4 
list1=[1, 2, 4, 4, 3, 3, 3, 6, 5]
set1 = set(list1)
print("\nSet with the use of Numbers: ")
# after coverting listo to set. duplicate values are removed.
print(set1)



# +
# we can use this property to check if a list contains duplicate value
def check_duplicate(list):
    if len(list)>len(set(list)):
        print(f"Input list: {list} has duplicate")

check_duplicate(list1)
# -

# We can mix various data type in a set
set1 = set([1,"Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)

# ## 3.4.2 Adding Elements to a Set
# - add() : add one element to a set
# - update(): add a list, set, string, tuple to a set
#

# ### 3.4.2.1 Using add method
#
# The add() function adds one element at a time to the set, loops are used to add multiple elements
#
# - Lists cannot be added to a set as elements because Lists are mutable and not hashable
# - Tuples can be added because tuples are immutable and hence Hashable. 

# +
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
 
# Adding element and tuple to the Set
set1.add(8)
set1.add(9)
set1.add((6,7))
print("\nSet after Addition of Three elements: ")
print(set1)
 
# Adding elements to the Set
# using Iterator
for i in range(1, 6):
    set1.add(i)
print("\nSet after Addition of elements from 1-5: ")
print(set1)
# -

# ### 3.4.2.2 Using update() method
# Update() method can add two or more elements. It accepts lists, strings, tuples as well as other sets as its arguments. In all of these cases, duplicate elements are removed.
#
#

# +
# Addition of elements to the Set
# using Update function
set1 = set([ 4, 5, (6, 7)])
set1.update([10, 11])
print("\nSet after Addition of elements using Update: ")
print(set1)

# note even we give a tuple (6,7) as input. The update method will read element one by one. not use the tuple as one object
# as a result, below code give us {4, 5, 6, 7, 10, 11, (6, 7)} instead of {4, 5, 10, 11, (6, 7)}
set1.update((6,7))
print("\nSet after Addition of elements using Update: ")
print(set1)
# -

# ## 3.4.3 Accessing a Set
#
# **Set items cannot be accessed by referring to an index, since sets are unordered the items has no index**. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

print("\nElements of set: ")
for i in set1:
    print(i, end=" ")

# +
# check if an element is in the set
test1 = 1 in set1
print(f"result of test1: {test1}")

test2 = (6,7) in set1
print(f"result of test2: {test2}")
# -

# ## 3.4.4 Removing elements from the Set
#
# - remove(value): removes an element that matches the input value, but a KeyError arises if such element doesn’t exist in the set.
# - discard(value): removes an element from a set without KeyError, if the element doesn’t exist in the set, it remains unchanged.
# - pop(): removes an element and return it. remove and discard does not return any value.
# - clear(): removes all the elements from the set. 
#
# ### 3.4.4.1 Using remove()

# +
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)
 
# Removing elements from Set using Remove() method
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)
# -

# let's try to remove a value that does not exist
try:
    set1.remove(5)
except KeyError as e:
    print(f"error message: {e}")


# ### 3.4.4.2 Using discard()

# +
# Removing elements from Set using Discard() method
set1.discard(8)
element2=set1.discard(9)

print("Set after Discarding two elements: ")
print(set1)

# -

# let's try to discard a value that does not exist
# you can notice no error is raised
set1.discard(5)
print("Set after Discarding element 5: ")
print(set1)

# ### 3.4.4.3 Using pop() 
#
# **Pop() removes an element from the set and returns it, but it removes only the last element of the set.** 
#
# If the set is unordered then there’s no such way to determine which element is popped by using the pop() function. 

# +
# Creating a Set, note there is auto sorting after converting list to set
set1 = set([1, 2, 5, 6, 7, 8, 9, 3, 4,  10, 11, 12])
print("Initial Set: ")
print(set1)
 
# Removing element from the Set using the pop() method
for i in range(1,13):
    element=set1.pop()
    print(f"the element value that poped out of the set: {element} ")
    print(f"the set value is {set1}")

# +
#Creating a set
set1 = set([1,2,3,4,5])
print("\nInitial set: ")
print(set1)
 
# Removing all the elements from Set using clear() method
set1.clear()
print("\nSet after clearing all the elements: ")
print(set1)
# -

# ## 3.4.5 Frozen sets 
#
# In Python fronzenset are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. 
#
# While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. 
#
# If no parameters are passed, it returns an empty frozenset.  

# +
# Creating a Set
String = ('G', 'e', 'e', 'k', 's', 'F', 'o', 'r')
 
Fset1 = frozenset(String)
print("The FrozenSet from a string is: ")
print(Fset1)

list1=[1, 2, 5, 6, 7, 8, 9, 3, 4,  10, 11, 12]
Fset2=frozenset(list1)
print("The frozenSet from a list is: ")
print(Fset2)

# To print Empty Frozen Set No parameter is passed
print("\nEmpty FrozenSet: ")
print(frozenset())
# -

# try to add a new element in fronzenset
Fset2.add(13)



StarcoderdataJupyterScriptsDedupFiltered