Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 816 Bytes

17-set.md

File metadata and controls

42 lines (30 loc) · 816 Bytes

Commands used in the Egghead.io Series Intro to Python

Lesson Seventeen - Create Uniqu Unordered Collections in Python with Set

Create a set in Python:

animals = {'monkey', 'bear', 'dog', 'monkey', 'cat', 'bear', 'gorilla'}
animals # returns {'monkey', 'bear', 'gorilla', 'dog', 'cat'}

Check if a value is present in a set:

'monkey' in animals # returns True
'shark' in animals # returns False

Create an empty set in Python:

fish = set()

Add items to a set:

fish.add('shark')
fish.add('guppy')
fish.add('whale')

Remove items from a set:

fish.remove('whale')

Use union to combine two sets into a single, deduplicated set:

fish.union(animals) # returns {'guppy', 'monkey', 'bear', 'shark', 'gorilla', 'dog', 'cat'}