🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The operator.contains function in Python's operator module checks if an element is present in a container. It is equivalent to using the in operator but allows the membership test to be used as a function, which can be useful in functional programming and higher-order functions.
Table of Contents
- Introduction
operator.containsFunction Syntax- Examples
- Basic Usage
- Checking in Lists
- Checking in Strings
- Checking in Dictionaries
- Real-World Use Case
- Conclusion
Introduction
The operator.contains function checks if an element is present in a container (such as a list, string, or dictionary). It returns True if the element is found and False otherwise. This function is useful when you need to pass the membership test as a function to other functions or use it in functional programming contexts.
operator.contains Function Syntax
Here is how you use the operator.contains function:
import operator
result = operator.contains(container, element)
Parameters:
container: The container to be searched (such as a list, string, or dictionary).element: The element to search for in the container.
Returns:
Trueif the element is found in the container,Falseotherwise.
Examples
Basic Usage
Check if an element is present in a list using operator.contains.
Example
import operator
container = [1, 2, 3, 4, 5]
element = 3
result = operator.contains(container, element)
print(f"contains({container}, {element}) = {result}")
Output:
contains([1, 2, 3, 4, 5], 3) = True
Checking in Lists
Check if an element is present in a list.
Example
import operator
numbers = [10, 20, 30, 40, 50]
value = 25
result = operator.contains(numbers, value)
print(f"contains({numbers}, {value}) = {result}")
Output:
contains([10, 20, 30, 40, 50], 25) = False
Checking in Strings
Check if a substring is present in a string.
Example
import operator
string = "Hello, world!"
substring = "world"
result = operator.contains(string, substring)
print(f"contains({string}, '{substring}') = {result}")
Output:
contains(Hello, world!, 'world') = True
Checking in Dictionaries
Check if a key is present in a dictionary.
Example
import operator
dictionary = {"a": 1, "b": 2, "c": 3}
key = "b"
result = operator.contains(dictionary, key)
print(f"contains({dictionary}, '{key}') = {result}")
Output:
contains({'a': 1, 'b': 2, 'c': 3}, 'b') = True
Real-World Use Case
Filtering Data
In data processing, you might need to filter data based on the presence of elements in a container. The operator.contains function can be used in a functional programming style to achieve this.
Example
import operator
data = ["apple", "banana", "cherry", "date"]
filter_values = {"banana", "date"}
filtered_data = list(filter(lambda x: operator.contains(filter_values, x), data))
print(f"Filtered data: {filtered_data}")
Output:
Filtered data: ['banana', 'date']
Conclusion
The operator.contains function is used for performing membership tests in a functional programming context in Python. It provides a way to use the in operator as a function, which can be passed to other functions or used in higher-order functions. By understanding how to use operator.contains, you can write more flexible and readable code that leverages functional programming techniques.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment