Python search in set

How to Check If a Set Contains an Element in Python

Here are 4 methods to check if a set contains an element in Python:

  1. Using the “in” operator
  2. Using the “not in” operator
  3. Using the combination of “Counter()” and “dict.keys()” methods
  4. Using “operator.countOf()” method

Method 1: Using the “in” operator

To check if the Set contains an element in Python, you can use the “in operator”, which returns True if the specified Set contains an element and False otherwise. The “in operator” checks if an element is in a sequence like a list, range, string, or set.

Example

first_set = el_in_set = 19 in first_set print(el_in_set)

It returns True because the 19 is included in the Set.

Check for element 46 in the existing Set and see the output.

first_set = el_in_set = 46 in first_set print(el_in_set)

We got False because the Set does not contain the “46” element.

Method 2: Using the “not in” operator

The “not in” operator in Python works the opposite way as the in operator works precisely. The “not in” operator checks the presence of a specified value inside a given sequence, but its return values are opposite to that of the in operator.

Example

first_set = el_in_set = 46 not in first_set print(el_in_set)

It returns True because the Set does not contain 46. That is why not in operator returns True because it is not in the Set. Let’s take an example where the element exists in the Set and see the output.

first_set = el_in_set = 11 not in first_set print(el_in_set)

And it returns False, which is correct because 11 exists in the Set.

Method 3: Using the Counter() Function

The Counter is a dict subclass, and collections module method used to count the occurrences of elements in an iterable. It can help count elements, notably when you have many elements that are either repeated or unique.

Example

from collections import Counter set = element_dict = Counter(set) print(21 in element_dict.keys()) print(20 in element_dict.keys()) print(19 in element_dict.keys())

The Time Complexity is O(N), and the Auxiliary Space complexity is O(N).

Method 4: Using the operator.countOf() method

import operator as op data = print(op.countOf(data, 1001) > 0) print(op.countOf(data, 1011) > 0) print(op.countOf(data, 7071) > 0) 

Conclusion

The best and most efficient way to check if a set contains an element is to use the “in operator” in Python.

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Python search in set

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.

Источник

Читайте также:  Auto javascript code suggestions
Оцените статью