Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
140 views

Python Sets: "Apple" "Banana" "Cherry"

Sets in Python are used to store unordered and unindexed collections of unique elements. Sets cannot contain duplicate values. Common set methods allow you to add elements, remove elements, join sets, check membership, and iterate over elements.

Uploaded by

Anjushree S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Python Sets: "Apple" "Banana" "Cherry"

Sets in Python are used to store unordered and unindexed collections of unique elements. Sets cannot contain duplicate values. Common set methods allow you to add elements, remove elements, join sets, check membership, and iterate over elements.

Uploaded by

Anjushree S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Sets

myset = {"apple", "banana", "cherry"}

Set

Sets are used to store multiple items in a single


variable.

Set is one of 4 built-in data types in Python used to


store collections of data, the other 3 are List, Tuple,
and Dictionary, all with different qualities and usage.

A set is a collection which


is unordered, unchangeable*, and unindexed.

* Note: Set items are unchangeable, but you can


remove items and add new items.

Sets are written with curly brackets.

Example

Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
Set Items
Set items are unordered, unchangeable, and do not
allow duplicate values.

Unordered

Unordered means that the items in a set do not have a


defined order.

Set items can appear in a different order every time


you use them, and cannot be referred to by index or
key.

Unchangeable

Set items are unchangeable, meaning that we cannot


change the items after the set has been created.

Once a set is created, you cannot change its items, but


you can remove items and add new items.

Duplicates Not Allowed

Sets cannot have two items with the same value.

Example

Duplicate values will be ignored:


thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)
Get the Length of a Set

To determine how many items a set has, use


the len() function.

Example

Get the number of items in a set:


thisset = {"apple", "banana", "cherry"}

print(len(thisset))

The set() Constructor


It is also possible to use the set() constructor to make
a set.

Example

Using the set() constructor to make a set:


thisset = set(("apple", "banana", "cherry")) # note
the double round-brackets
print(thisset)

Output
{'apple', 'banana', 'cherry'}

Python - Access Set Items


Access Items
You cannot access items in a set by referring to an
index or a key.

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.

Example

Loop through the set, and print the values:


thisset = {"apple", "banana", "cherry"}

for x in thisset:
print(x)
apple
banana
cherry

Python - Add Set Items

Add Items
Once a set is created, you cannot change its items, but
you can add new items.

To add one item to a set use the add() method.


Example

Add an item to a set, using the add() method:


thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)

output
{'apple', 'cherry', 'orange', 'banana'}

Add Sets
To add items from another set into the current set, use
the update() method.

Example

Add elements from tropical into thisset:


thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset)

Output
{'apple', 'mango', 'cherry', 'pineapple', 'banana',
'papaya'}
Add Any Iterable
The object in the update() method does not have to be
a set, it can be any iterable object (tuples, lists,
dictionaries etc.).

Example

Add elements of a list to at set:


thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]

thisset.update(mylist)

print(thisset)

output

{'banana', 'cherry', 'apple', 'orange', 'kiwi'}

Python - Remove Set Item

Remove Item
To remove an item in a set, use the remove(), or
the discard() method.

Example
Remove "banana" by using the remove() method:
thisset = {"apple", "banana", "cherry"}

thisset.remove("banana")

print(thisset)

output
{'cherry', 'apple'}

Note: If the item to remove does not


exist, remove() will raise an error.
Example

Remove "banana" by using the discard() method:


thisset = {"apple", "banana", "cherry"}

thisset.discard("banana")

print(thisset)

output
{'cherry', 'apple'}

Note: If the item to remove does not


exist, discard() will NOT raise an error.

You can also use the pop() method to remove an item,


but this method will remove the last item. Remember
that sets are unordered, so you will not know what
item that gets removed.
The return value of the pop() method is the removed
item.

Example

Remove the last item by using the pop() method:


thisset = {"apple", "banana", "cherry"}

x = thisset.pop()

print(x)

print(thisset)

ouput
banana
{'cherry', 'apple'}

Note: Sets are unordered, so when using


the pop() method, you do not know which item that
gets removed.
Example

The clear() method empties the set:


thisset = {"apple", "banana", "cherry"}

thisset.clear()

print(thisset)

output
set()

The del keyword will delete the set completely:


thisset = {"apple", "banana", "cherry"}

del thisset

print(thisset)

Python - Loop Sets

Loop Items

You can loop through the set items by using a for loop:

Example

Loop through the set, and print the values:


thisset = {"apple", "banana", "cherry"}

for x in thisset:
print(x)

Python - Join Sets

Join Two Sets


There are several ways to join two or more sets in
Python.

You can use the union() method that returns a new set
containing all items from both sets, or
the update() method that inserts all the items from one
set into another:

Example

The union() method returns a new set with all items


from both sets:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set3 = set1.union(set2)
print(set3)

output

{1, 'b', 2, 'c', 'a', 3}

The update() method inserts the items in set2 into


set1:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)

output
{'b', 1, 'a', 3, 2, 'c'}

Note: Both union() and update() will exclude any


duplicate items.

You might also like