- Set python get item
- Constructors¶
- Methods¶
- Adding Elements¶
- Deleting¶
- Information¶
- Set Operations¶
- Set Operations Assignment¶
- Copying¶
- Set Operators¶
- Adding Elements¶
- Relational Operators¶
- Set Operations¶
- Set Operations Assignment¶
- Functions¶
- Python — Access Set Items
- Example
- Example
- Change Items
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
Set python get item
Sets are mutable unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.
Sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.
Sets are implemented using dictionaries. They cannot contain mutable elements such as lists or dictionaries. However, they can contain immutable collections.
Constructors¶
set() Returns a set type initialized from iterable. <> set comprehension Returns a set based on existing iterables. literal syntax Initializes a new instance of the set type.
Methods¶
Adding Elements¶
Deleting¶
discard Removes an element from the set. remove Removes an element from the set (raises KeyError if not found). pop Removes and returns an arbitrary element from the set. clear Removes all elements from the set.
Information¶
issuperset Returns a Boolean stating whether the set contains the specified set or iterable. issubset Returns a Boolean stating whether the set is contained in the specified set or iterable. isdisjoint Returns a Boolean stating whether the set contents do not overlap with the specified set or iterable.
Set Operations¶
difference Returns a new set with elements in the set that are not in the specified iterables. intersection Returns a new set with elements common to the set and the specified iterables. symmetric_difference Returns a new set with elements in either the set or the specified iterable but not both. union Returns a new set with elements from the set and the specified iterables.
Set Operations Assignment¶
difference_update Updates the set, removing elements found in the specified iterables. intersection_update Updates the set, keeping only elements found in it and the specified iterables. symmetric_difference_update Updates the set, keeping only elements found in either set or the specified iterable, but not in both.
Copying¶
Set Operators¶
Adding Elements¶
Relational Operators¶
== (is equal) Returns a Boolean stating whether the set has the same elements as the other set. != (is not equal) Returns a Boolean stating whether the set has different elements as the other set. = (issuperset) Returns a Boolean stating whether the set contains the other set. > (issuperset proper) Returns a Boolean stating whether the set contains the other set and that the sets are not equal.
Set Operations¶
— (difference) Returns a new set with elements in the set that are not in the other set. & (intersection) Returns a new set with elements common to the set and the other set. ^ (symmetric_difference) Returns a new set with elements in either the set or the other set but not both. | (union) Returns a new set with elements from the set and the other set.
Set Operations Assignment¶
-= (difference_update) Updates the set, removing elements found in the other set. &= (intersection_update) Updates the set, keeping only elements found in it and the other set. ^= (symmetric_difference_update) Updates the set, keeping only elements found in either set or the other set, but not in both.
Functions¶
len Returns an int type specifying number of elements in the collection. min Returns the smallest item from a collection. max Returns the largest item in an iterable or the largest of two or more arguments. sum Returns a total of the items contained in the iterable object. sorted Returns a sorted list from the iterable. reversed Returns a reverse iterator over a sequence. all Returns a Boolean value that indicates whether the collection contains only values that evaluate to True. any Returns a Boolean value that indicates whether the collection contains any values that evaluate to True. enumerate Returns an enumerate object. zip Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
© Copyright 2015, Jakub Przywóski. Revision 9a3b94e7 .
Versions latest Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.
Python — Access Set Items
You cannot access items in a set by referring to an index or a key.
But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.
Example
Loop through the set, and print the values:
Example
Check if «banana» is present in the set:
Change Items
Once a set is created, you cannot change its items, but you can add new items.
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.