Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Python - Set

Back


Set


Set Methods

Method Description
set() return a set
Method Description
set() return a set
copy() Returns a copy of the set
Method Description
add() Adds an element to the set
Method Description
update() Update the set with the union of this set and others
difference_update() Removes the items in this set that are also included in another, specified set
intersection_update() Removes the items in this set that are not present in other, specified set(s)
symmetric_difference_update() inserts the symmetric differences from this set and another
Method Description
pop() Removes an element from the set
remove() Removes the specified element
discard() Remove the specified item
clear() Removes all the elements from the set
Method Description
difference() Returns a set containing the difference between two or more sets
intersection() Returns a set, that is the intersection of two other sets
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
symmetric_difference() Returns a set with the symmetric differences of two sets
union() Return a set containing the union of sets

Create a set

# create a set

print("\n--------Create a Set--------\n")
# using curly brackets
xset = {"apple", "banana", "cherry"}
print(xset)     # {'apple', 'banana', 'cherry'}


# using set() constructor
xset = set(("apple", "banana", "cherry"))  # note the double round-brackets
print(xset)     # {'cherry', 'banana', 'apple'}

Add Items

add(): adds an element

print("\n--------Add a Item:add()--------\n")
xlist = {"apple", "banana", "cherry"}
xlist.add("orange")
print(xlist)    # {'cherry', 'orange', 'apple', 'banana'}

update(): adding items from another iterable.

print("\n---------Add items: update()--------\n")
xset = {"apple", "banana", "cherry"}
yset = {"pineapple", "mango", "papaya"}
xset.update(yset)

print(xset)     # {'papaya', 'banana', 'apple', 'cherry', 'pineapple', 'mango'}

zset = xset.update(yset)
print(zset)     # None

# iterable object
xset = {"apple", "banana", "cherry"}
yset = ["pineapple", "mango", "papaya"]
xset.update(yset)

print(xset)     # {'papaya', 'banana', 'apple', 'cherry', 'pineapple', 'mango'}

Remove Item

discard(value)

# .discard()
xset = {"apple", "banana", "cherry"}
xset.discard("banana")
print(xset)

xset.discard("banana")
xset.discard("banana")

pop()

# .pop()
xset = {"apple", "banana", "cherry"}
x = xset.pop()            #
# x = xset.pop(1)           # TypeError: set.pop() takes no arguments (1 given)
# x = xset.pop("banana")    # TypeError: set.pop() takes no arguments (1 given)
print(xset)
print(x)                    # a random item removed from set

remove(item)

# .remove()
xset = {"apple", "banana", "cherry"}
xset.remove("banana")
print(xset)

# xset.remove("banana")       # KeyError: 'banana'

clear()

# clear()
xset = {"apple", "banana", "cherry"}
xset.clear()

print(xset)     # set()

del Operator

# del
xset = {"apple", "banana", "cherry"}
del xset

# print(xset)     # NameError: name 'xset' is not defined

copy(): copies the set

x_set = {"apple", "banana", "cherry"}
y_set = x_set.copy()
print(y_set)    # {'banana', 'apple', 'cherry'}

Loop Set


print("\n--------for in loop--------\n")
# for in loop
xset = {"apple", "banana", "cherry"}
for x in xset:
    print(x)


in Operator: Check if Item Exists

xlist = {"apple", "banana", "cherry"}
if "apple" in xlist:
  print("Yes, 'apple' is in the fruits list")

TOP