What is the set used for in Python?

8

What is and what is set in Python?

test = set([1, 2, 3]);
    
asked by anonymous 31.07.2015 / 21:10

1 answer

12

Set is a collection that represents set.

This collection has the characteristic to be disordered and have the elements unique, that is, there is no repetition of elements in this collection type.

There are also methods that do assembly operations, such as union and intersection that respectively return all elements of two set and the elements they have in common.

>>> a = {1, 2, 3, 4}
>>> b = {3, 4, 5, 6}
>>> print a.union(b)
set([1, 2, 3, 4, 5, 6])

>>> l1 = [1, 2, 3]
>>> l2 = [2, 4, 3]
>>> print set(l1).intersection(l2)
set([2, 3])

Reference link

    
31.07.2015 / 21:27