
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can we speed up Python "in" operator?
The python operator performs very badly in a list, O(n), because it traverses the whole list. You can use something like a set or a dict(hashed data structures that have very fast lookups) to get the same result in ~O(1) time!
But this also depends on the type of data structure you're looking at. This is because while lookups in sets/dicts are fast, insertion may take more time than list. So this speedup really depends on the type.
- Related Articles
- Can we change operator precedence in Python?
- How can we use Python Ternary Operator Without else?
- How can we do Python operator overloading with multiple operands?
- How can we use MySQL UNION operator on datasets?
- How do we use equivalence (“equality”) operator in Python classes?
- Python program to check whether we can pile up cubes or not
- How to speed up SELECT DISTINCT in MySQL
- How to speed up $group phase in aggregation?
- My Python program is too slow. How do I speed it up?
- How do we access class attributes using dot operator in Python?
- Python Pandas – Can we use & Operator to find common columns between two DataFrames?
- How can we use a diamond operator with anonymous classes in Java 9?
- How to Speed up Gradle build process in Android Studio?
- Program to check whether we can pick up and drop every passenger in given list in Python
- How we can import Python modules in Jython?

Advertisements