Home > Backend Development > Python Tutorial > Python program to test if any set element is present in a list

Python program to test if any set element is present in a list

WBOY
Release: 2023-09-04 21:49:04
forward
1207 people have browsed it

Python program to test if any set element is present in a list

In this article, we will learn how to check if any set element exists in a list in python.

usage instructions

  • Use any() function

  • Use the bitwise & operator

  • Using Counter(), filter() and lambda functions

Example

Assume we have taken input set and input list . We will now check if any input set element exists in the input list using the above method.

enter

inputSet = {4, 8, 1, 3, 5, 7}
inputList = [7, 15, 20]
Copy after login

Output

Checking whether any set element present in the input list: True
Copy after login

In the above example, 7 exists in both the set and the list, so the result is True

Method 1: Use any() function

If any item in the iterable object is true, the any() function returns True, otherwise it returns False.

grammar

any(iterable)
Copy after login

Algorithm (steps)

Following are the algorithms/steps that need to be followed to perform the required task -.

  • Create a variable to store the input set and print the given set.

  • Create another variable to store the input list.

  • Use the any() function to check whether any collection elements are present in the input list by looping through the input collection and checking whether the current element is present in the input list.

  • Print the result as a Boolean value.

Example

The following program uses the any() function to check whether any input set element exists in the input list, and returns True if it exists, otherwise it returns False –

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [7, 15, 20]

# checking whether any set element is present in the input list using any() function
result = any(i in inputSet for i in inputList)

# printing the output
print("Checking whether any set element present in the input list:", result)
Copy after login

Output

When executed, the above program will generate the following output -

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True
Copy after login
Copy after login

Method 2: Using the bitwise & operator

Bitwise & Operator - "&" is a bitwise operator that compares numbers (binary). If both bits are 1, then it sets each bit to 1.

Algorithm (steps)

The following are the algorithms/steps that need to be followed to perform the required task -

  • Use the set() function to convert the given input into a set.

  • Check if there are any set elements in the input list using the & operator (if both bits are 1, set each bit to 1) and use bool() Function (returns the Boolean value of the given object)

  • Print the results.

Example

The following program uses bitwise & operators to check if any input set element exists in the input list and returns True if present otherwise False –

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [9, 15, 20]

# Convert the given list to set using the set() function
inputListSet = set(inputList)

# checking whether any set element present in the input list

# using & operator(checks for common element) and converting to boolean
result = bool(inputSet & inputListSet)

# printing the output
print("Checking whether any set element present in the input list:", result)
Copy after login

Output

When executed, the above program will generate the following output -

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: False
Copy after login

Method 3: Using Counter(), filter() and lambda functions

filter() function - Filters a specified sequence using a function that determines whether each element in the sequence is true or false.

Counter() function - Counts subclasses of hashable objects. It implicitly creates an iterable hash table when called/invoked.

lambda() function

The lambda function is a small anonymous function.

lambda function can have unlimited/any number of parameters, but can only have one expression.

grammar

lambda arguments : expression
Copy after login

Algorithm (steps)

The following are the algorithms/steps that need to be followed to perform the required task -

  • Use the import keyword to import the Counter function from the collection module.

  • Use the Counter() function to get the frequency of all input list elements in dictionary form.

  • Use the filter function to filter all input set elements if they are present in the above frequency dictionary.

  • The length of the filtered list will be greater than 1 if there are any common elements.

  • Use if conditional statements to check whether the above conditions are true and print accordingly.

Example

The following program uses Counter(), filter() and lambda functions to check if any input set element exists in the input list and returns True if present otherwise False –

# importing a Counter function from the collections module
from collections import Counter

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [7, 15, 20, 7]

# getting the frequency of list elements using the Counter() function

# Here it returns frequencies as a dictionary
elements_freq = Counter(inputList)

# Traversing in the input Set using the lambda function

# Checking if the set element exists in the keys of the dictionary

# Filtering all the elements which satisfy the above condition
output = list(filter(lambda k: k in elements_freq.keys(), inputSet))

# Check if there are any filtered elements
if(len(output) > 0):
   output = True

# If no elements are common then the output will be False
else:
   output = False

# printing the output
print("Checking whether any set element present in the input list:", output)
Copy after login

Output

When executed, the above program will generate the following output -

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True
Copy after login
Copy after login

in conclusion

In this article, we learned how to use three different methods to determine whether a set contains an element in a list. We also learned how to convert any iterable object such as a list, tuple, or any iterable object into a set using the set() function and how to use the & operator to find elements common to both sets given a given set.

The above is the detailed content of Python program to test if any set element is present in a list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template