python

To share this page click on the buttons below;

Set

Set is an unordered collection of elements. Each element of the set is unique and not mutable, but the set itself is mutable.

Sets are created with

<myset> = {<element1>, ...., <element_n>};

or with the function set()

<mylist> = set();

Since {} are used for dictionaries to define an empty set you must use the function set().

Add

To add an element

<mylist>.add(<element>)

To add more elements

<mylist>.update(<element>,<element>,...)

Delete

Discard do not throw exception if element does not exists

<mylist>.discard(<element>)

Remove throws exception if element does not exists

<mylist>.remove(<element>)

Pop removes a random element

<mylist>.pop()

<dict>.items() returns a list of couples <key,value>

Collections

Collections are containers used to stores elements. To use collections uses the collections module.

Counter

Counter is a functions that takes as input a list or a dictionary and returns a dictionary which has as keys the elements of the list or the keys of the dictionary and as value the number of occurrence of the elements or keys.

To share this page click on the buttons below;