** in argument lists has a special meaning, as covered in section 4.7 of the tutorial. The dictionary (or dictionary-like) object passed with **kwargs is expanded into keyword arguments to the callable, much like *args is expanded into separate positional arguments.

Answer from Thomas Wouters on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_ref_dictionary.asp
Python Dictionary Methods
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else · Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Matplotlib Intro Matplotlib Get Started Matplotlib Pyplot Matplotlib Plotting Matplotlib Markers Matplotlib Line Matplotlib Labels Matplotlib Grid Matplotlib Subplot Matplotlib Scatter Matplotlib Bars Matplotlib Histograms Matplotlib Pie Charts
Discussions

in operator for dictionary VS. in operator for dict.keys()
n in d and n in d.keys() do essentially the exact same thing. .keys() returns a dict_keys object, which behaves mostly set-like and is really just a view on its parent dictionary's state. Any difference in execution time is probably down to the overhead of the additional function call. Dictionaries in Python are implemented as hash maps. The key is converted to a number via a hashing function, and that number determines which of several buckets the key falls into. Computing the hashing function and determining the bucket that corresponds to the result can be done in constant time. If more than one key resides within the bucket, the correct one is determined via a linear search. However, with a sufficient number of buckets and a well chosen hashing function, the number of keys per bucket should be small enough that a dictionary lookup is still essentially constant time. More on reddit.com
🌐 r/learnpython
26
9
January 1, 2022
python - use "in" operator for dictionary in a class - Stack Overflow
I have a __contains__ method in class called "Graph" with an instance "dict" which is a dictionary. def __contains__(self,i): return i in self.dict Now I would like to use "in" to loop throu... More on stackoverflow.com
🌐 stackoverflow.com
optimization - Does the 'in' clause used on python dictionaries call the keys() function every time? - Stack Overflow
So use the built-in sets and its overloading of the & operator for intersections: for word in set(some_dict) & set(wordlist): # do something ... I realize I'm not answering the question regarding optimization and it's possible that, with very long lists and large dictionaries, then having Python ... More on stackoverflow.com
🌐 stackoverflow.com
What exactly is the "*" operator doing here:
The *, or 'splat' operator, unpacks a sequence and passes it to a function as individual values. For example: a = [1, 2, 3] print(*a) --> print(1, 2, 3) This is useful when you have an iterable but you need to pass it to a function that will only accept individual values. More on reddit.com
🌐 r/learnpython
39
175
March 8, 2020
🌐
Note.nkmk.me
note.nkmk.me › home › python
The in Operator in Python (for List, String, Dictionary) | note.nkmk.me
May 9, 2023 - In Python, the in and not in operators test membership in lists, tuples, dictionaries, and so on. 6. Expressions - Membership test operations — Python 3.11.2 documentation How to use the in operatorB ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-operators-for-sets-and-dictionaries
Python Operators for Sets and Dictionaries - GeeksforGeeks
May 1, 2025 - These operators help with retrieving, updating, comparing and checking the existence of keys. Let’s explore them. These are used to get, set, or delete values in a dictionary.
🌐
Python
peps.python.org › pep-0584
PEP 584 – Add Union Operators To dict | peps.python.org
PEP 584: Add + and += operators to the built-in dict class. ... Merging two dictionaries in an expression is a frequently requested feature.
🌐
Reddit
reddit.com › r/learnpython › in operator for dictionary vs. in operator for dict.keys()
r/learnpython on Reddit: in operator for dictionary VS. in operator for dict.keys()
January 1, 2022 -

Main Questions are at end of post...

I'm going over the Two Sum problem on LeetCode, and the optimal solution was to use a dictionary. This involved a "look up" to see if a dictionary key existed. Can someone explain to me how exactly python "looks up" a dictionary key to see if it exists. Particularly in comparing these two lines of code:

# where n is a key, and d is a dictionary
if n in d:

# where n is a key, and d.keys() is a viewing object (a list??)
if n in d.keys():

I was confused because these two methods took around the same time on leetcode, and I thought the second method would have a time complexity of O(n) because (I thought) it was just a list.

I did further testing with the timeit module... my code and its results are below:

# checking time complexity of dict key look up methods
# basically I'm running both methods and checking how much time each takes as the dictionary grows
# I thought that the second method would take longer and longer as the dictionary grew, but that doesn't seem to be the case

Code:

from timeit import Timer

in_op = Timer("x in d", "from __main__ import x, d")
key_method = Timer("x in d.keys()", "from __main__ import x, d")

for i in range(1_000_000, 10_000_001, 1_000_000):
	x = i
	d = {i:0 for i in range(i)}
	d[i] = 'yes'
	in_op_time = in_op.timeit(1000)
	d = {i:0 for i in range(i)}
	d[i] = 'yes'
	key_method_time = key_method.timeit(1000)
	print(f"{in_op_time=} and {key_method_time=}")

Results:

in_op_time=3.087499999999965e-05 and key_method_time=7.250000000000312e-05
in_op_time=3.008300000001407e-05 and key_method_time=6.941599999998882e-05
in_op_time=2.9749999999939547e-05 and key_method_time=6.862500000004434e-05
in_op_time=3.0790999999918967e-05 and key_method_time=6.945800000002222e-05
in_op_time=3.0165999999942628e-05 and key_method_time=7.025000000027148e-05
in_op_time=3.0250000000009436e-05 and key_method_time=6.829099999983157e-05
in_op_time=3.00840000000413e-05 and key_method_time=6.845799999988245e-05
in_op_time=2.958299999988867e-05 and key_method_time=6.824999999999193e-05
in_op_time=2.954200000004903e-05 and key_method_time=6.891600000002995e-05
in_op_time=2.9916999999990423e-05 and key_method_time=6.833300000064213e-05

My questions are:

  1. How does the first case "know" that the key doesn't exist? I understand when you try to access a key that doesn't exist, python gives an error, so I'm assuming that's the same mechanism?

  2. If the dict.keys() doesn't return a list, then what does it return?

  3. How does the in operator work with dict.keys()?... Like how does it bypass the returned object and go straight to looking up the key with normal dictionary methods?

Find elsewhere
🌐
Real Python
realpython.com › lessons › operators-and-methods-dictionaries-python
Operators and Methods for Dictionaries in Python (Video) – Real Python
You can use the in operator together with short-circuit evaluation to avoid raising an error when trying to access a key that is not in the dictionary. The len() function returns the number of key-value pairs in a dictionary. To learn more about dictionaries, you might want to check out Python Basics: Dictionaries.
Published   July 30, 2019
🌐
Real Python
realpython.com › python-dicts
Dictionaries in Python – Real Python
December 16, 2024 - The membership operators in and not in allow you to determine whether a given key, value, or item is in a dictionary, depending on the target iterable you use. Note: To learn more about membership tests, check out Python’s “in” and “not in” Operators: Check for Membership.
🌐
Mimo
mimo.org › glossary › python › in-operator
Master Python's in Operator for Various Coding Scenarios
Checks Keys in Dictionaries: When used with a dictionary, the in operator checks for the presence of a key, not a value. It's Case-Sensitive: When checking strings, 'a' in 'Apple' will be False. You must convert both sides to the same case (e.g., .lower()) for a case-insensitive check.
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
For example, if A and C are true but B is false, A and B and C does not evaluate the expression C. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument. It is possible to assign the result of a comparison or other Boolean expression to a variable. For example, >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' >>> non_null = string1 or string2 or string3 >>> non_null 'Trondheim' Note that in Python, unlike C, assignment inside expressions must be done explicitly with the walrus operator :=. This avoids a common class of problems encountered in C programs: typing = in an expression when == was intended.
🌐
W3Schools
w3schools.com › python › python_operators.asp
Python Operators
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else · Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Matplotlib Intro Matplotlib Get Started Matplotlib Pyplot Matplotlib Plotting Matplotlib Markers Matplotlib Line Matplotlib Labels Matplotlib Grid Matplotlib Subplot Matplotlib Scatter Matplotlib Bars Matplotlib Histograms Matplotlib Pie Charts
🌐
Sololearn
sololearn.com › en › Discuss › 152566 › how-in-keyword-in-python-works
How 'in' keyword in Python works? | Sololearn: Learn to code for FREE!
January 3, 2017 - As the list gets longer, the search time gets longer in direct proportion. For dictionaries, Python uses an algorithm called a hashtable that has a remarkable property: the in operator ...
🌐
AskPython
askpython.com › home › python “in” and “not in” membership operators: examples and usage
Python "in" and "not in" Membership Operators: Examples and Usage - AskPython
January 25, 2026 - Python calls the __eq__ method ... Strings behave differently than you might expect. The in operator checks for substring matches, not just single characters....
🌐
SQLPad
sqlpad.io › tutorial › python-in-operator
Master the Python 'in' operator to enh…
April 29, 2024 - This simplicity makes it one of the most frequently used operators in Python. Here's a basic example of the 'in' operator in action: # Check if a number is in a list numbers = [1, 2, 3, 4, 5] print(3 in numbers) # Output: True # Check if a character is in a string greeting = "Hello, World!" print('H' in greeting) # Output: True # Check if a key is in a dictionary user_info = {'name': 'Alice', 'age': 25} print('name' in user_info) # Output: True
🌐
GeeksforGeeks
geeksforgeeks.org › python › time-complexity-of-in-operator-in-python
Time Complexity of In Operator in Python - GeeksforGeeks
July 23, 2025 - The in operator checks if a key exists in the dictionary, providing average case constant time complexity. ... The average case time complexity is O(1) for key lookups due to the efficient hashing mechanism.
🌐
Saurus
saurus.ai › home › the python ‘in’ operator
The Python in Operator Clearly Explained
October 7, 2024 - In strings, the in operator checks for substrings. line = "One fish, two fish, red fish, blue fish" print("fish" in line) # --> True print("cat" in line) # --> False · In dictionaries, the in operator checks for the presence of a key.
🌐
Python documentation
docs.python.org › 3 › reference › expressions.html
6. Expressions — Python 3.14.3 documentation
1 month ago - The operators in and not in test for membership. x in s evaluates to True if x is a member of s, and False otherwise. x not in s returns the negation of x in s. All built-in sequences and set types support this as well as dictionary, for which ...
🌐
Quora
quora.com › Does-the-in-operator-while-searching-keys-in-Python-Dictionary-take-O-1-If-yes-how
Does the 'in' operator while searching keys in Python Dictionary take O(1)? If yes, how? - Quora
Answer (1 of 2): Yes. The way it works is by using a Hash Table as the underlying implementation for Python dictionaries. Hash tables have O(1) insertion, O(1) deletion, and O(1) search.