What’s the set for in Python?

Asked

Viewed 8,086 times

16

What is and what is the set in Python?

test = set([1, 2, 3]);

1 answer

20


Set is a Collection representing set.

This collection has the characteristic of being disorderly and having the unique elements, that is, there is no repetition of elements in this type of Collection.

There are also methods that do set 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 https://docs.python.org/2/library/sets.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.