ScanSkill

set

This set() function returns a set object i.e. it creates a set.

Syntax

set([iterable])

Here,

  • iterable: Optional. Any object which is iterable. Iterable may be either a sequence, a container that supports iteration, or an iterator object which can be converted into a set.

Note: You cannot create empty sets using {} syntax as it creates an empty dictionary. To create an empty set, you need to use set().

Examples

  • With string, tuple, list, and range
>>> # with string
>>> set('Cloudyfox')
{'o', 'x', 'f', 'y', 'u', 'C', 'd', 'l'}

>>> # with tuple
>>> set(('a', 'e', 'i', 'o', 'u')) 
{'i', 'o', 'a', 'u', 'e'}

>>> # with list
>>> set(['a', 'e', 'i', 'o', 'u'])
{'i', 'o', 'a', 'u', 'e'}

>>> # with range
>>> set(range(10))
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  • With set, dictionary, and frozen set
# With another set
>>> set({'o', 'x', 'f', 'y', 'u', 'C', 'd', 'l'})
{'o', 'x', 'f', 'y', 'u', 'C', 'd', 'l'}

>>> # with dictionary
>>> set({'a':1, 'b': 2, 'c':3, 'd':4})
{'c', 'a', 'd', 'b'}

>>> # with frozenset
>>> set(frozenset(('a', 'e', 'i', 'o', 'u')))
{'i', 'o', 'e', 'a', 'u'}