Skip to content

Instantly share code, notes, and snippets.

@msyvr
Last active December 17, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msyvr/364732600186ef2d20c947539a04e772 to your computer and use it in GitHub Desktop.
Save msyvr/364732600186ef2d20c947539a04e772 to your computer and use it in GitHub Desktop.
Python sets: unique, unsorted, unchangeable
def setpy():
"""
python sets are collections of objects which are:
unique
unordered
unchangeable*
* where unchangeable means individual items can't be replaced in place, but items can be removed and new items added
"""
# unique and unsorted
a = {4, 4, 2, 3}
print(a)
'''
prints to stdout:
{2, 3, 4}
nb: duplicate values have been omitted and items presented in ascending order, not the original ordering
'''
print(a.sorted())
'''
generates error message:
AttributeError: 'set' object has no attribute 'sorted'
'''
# unchangeable
a[2] = 7
print(a)
'''
generates error message:
TypeError: 'set' object does not support item assignment
'''
if __name__ == "__main__":
# function inputs - optional
# call function
setpy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment