Answers for "set in set python"

1

python string in set

a_string = 'Hello World Hello'

# In case we want to get a 'set' of chars
print(set(a_string))
# Output -> {'W', 'r', 'l', 'H', 'd', ' ', 'o', 'e'}

# In case we want to get a 'set' of words
print(set(a_string.split()))
# Output -> {'Hello', 'World'}
Posted by: Guest on June-22-2021
8

python set

# A set contains unique elements of which the order is not important
s = set()
s.add(1)
s.add(2)
s.remove(1)
print(s)
# Can also be created from a list (or some other data structures)
num_list = [1,2,3]
set_from_list = set(num_list)
Posted by: Guest on August-21-2020

Python Answers by Framework

Browse Popular Code Answers by Language