Python Language Set: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 7: Line 7:


=Overview=
=Overview=
=Sets and the <tt>__hash__()</tt> Function=
{{Internal|Eq_()_and_hash_()_in_Python#Dictionaries_and_the_hash_.28.29_Function|Sets and the <code>__hash__()</code> Function}}
=Initialization=
=Initialization=
Literal initialization:
Literal initialization:

Revision as of 16:11, 11 September 2022

Internal

TODO

Overview

Sets and the __hash__() Function

Sets and the __hash__() Function

Initialization

Literal initialization:

s = {1, 2, 3}

Set from a tuple:

s = set((1, 2, 3))

Set from a list:

s = set([1, 2, 3])

Set from a string:

s = set('ABC')
print(s)  # {'C', 'B', 'A'}

Shallow Copy

s = set()
s.add('a')
s2 = s.copy()

Remove an Element from a Set

s.remove('a')

Does not return anything.

Convert to List

s=set()
l=list(s)