convert list to set python code example

Example 1: add list to set python

set.update() or |=

>>> a = set('abc')
>>> l = ['d', 'e']
>>> a.update(l)
>>> a
{'e', 'b', 'c', 'd', 'a'}

>>> l = ['f', 'g']
>>> a |= set(l)
>>> a
{'e', 'b', 'f', 'c', 'd', 'g', 'a'}

Example 2: how to convert a set to a list in python

my_set = set([1,2,3,4])
my_list = list(my_set)
print my_list
>> [1, 2, 3, 4]

Example 3: convert list to set python

names = ['Barry', 'Alice', 'Bob', 'Bob']

unique_names = set(names)
# Result:
# {'Alice', 'Barry', 'Bob'}

Tags:

Java Example