⊗pyPmStCTS 124 of 208 menu

Converting to Set in Python

To convert a data type to a set, it should be passed to the set function parameter.

Let's make a set out of a string:

txt = 'abcde' st = set(txt) print(st) # {'a', 'b', 'c', 'e', 'd'}

Now let's transform the list into a set:

lst = [1, 2, 3, 4] st = set(lst) print(st) # {1, 2, 3, 4}

However, when transforming a dictionary, only its keys will be included in the set:

dct = { 'a': 1, 'b': 2, 'c': 3 } st = set(dct) print(st) # {'c', 'b', 'a'}

Two lines are given:

txt1 = '1234' txt2 = '5678'

Make one set out of them.

A tuple is given:

tlp = ('a', 'b', 'c', 'd')

Convert it to a set.

Given a dictionary:

dct = { 1: 'ab', 2: 'cd', 3: 'ef', 4: 'jh' }

Make two sets out of it. One set will contain the dictionary keys, and the other set will contain the values.

byenru