TypeError: 'in <string>' requires string as left operand, not list

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
8 min

banner

# Table of Contents

  1. TypeError: 'in <string>' requires string as left operand, not LIST
  2. TypeError: 'in <string>' requires string as left operand, not INT
  3. TypeError: 'in <string>' requires string as left operand, not dict

# TypeError: 'in <string>' requires string as left operand, not list

The Python "TypeError: 'in <string>' requires string as left operand, not list" occurs when we use the in operator with a left-hand side value of a list and a right-hand side value of a string.

To solve the error, move the list to the right-hand side of the in operator.

main.py
my_str = 'bobby' my_list = ['bobby', 'hadz', 'com'] # ⛔️ TypeError: 'in <string>' requires string as left operand, not list result = my_list in my_str

incorrect order in statement

We used the in operator with a list on the left-hand side and a string on the right-hand side which caused the error.

# Move the list to the right-hand side of the in operator

To solve the error, move the list to the right-hand side of the in operator.

main.py
# ✅ Checking if a value is in a list my_str = 'bobby' my_list = ['bobby', 'hadz', 'com'] result = my_str in my_list print(result) # 👉️ True if my_str in my_list: # 👇️ this runs print('The string is in the list') else: print('The string is NOT in the list')

move list to right hand side of in operator

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

main.py
my_list = ['bobby', 'hadz', 'com'] print('bobby' in my_list) # 👉️ True print('another' in my_list) # 👉️ False

# Checking if a value is NOT in a list

If you need to check if a string is NOT in a list, use the not in operator instead.

main.py
my_list = ['bobby', 'hadz', 'com'] print('bobby' not in my_list) # 👉️ False print('another' not in my_list) # 👉️ True # --------------------------------------- if 'another' not in my_list: # 👇️ This runs print('The value is NOT in the List') else: print('The value is in the List')

check if value is not in a list

x not in l returns the negation of x in l.

In other words, the not in operator will return True if the value is not in the list and False otherwise.

All built-in sequences and set types support the in and not in operators.

# Checking if a Substring is contained in a String

The in operator can also be used to check if a string contains a substring.

main.py
a_str = 'bobbyhadz.com' substring = '.com' if substring in a_str: # 👇️ This runs print('The substring is contained in the string') else: print('The substring is NOT contained in the string')

check if substring is contained in string

When used with two strings, the in operator returns True if the substring is contained in the string and False otherwise.

Note that the value on the left-hand side has to be of type string.

# Checking if a non-string value is contained in a String

If the value is an integer or any other type, you have to use the str() class to convert it to a string to be able to use the in operator.

main.py
a_str = 'bobbyhadz.com123456' an_int = 123 # ✅ Convert integer to string if str(an_int) in a_str: # 👇️ This runs print('The substring is contained in the string') else: print('The substring is NOT contained in the string')

check if non string value is contained in string

We used the str() class to convert the integer to a string to be able to use the in operator.

# Checking if a String is contained in a List ignoring the case

If you need to check if a string is contained in a list in a case-insensitive manner, convert the string and the list's items to lowercase.

main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'BOBBY' if my_str.lower() in (word.lower() for word in my_list): # 👇️ This runs print('The string is contained in the list') else: print('The string is not contained in the list')

The expression will return True if the string is contained in the list in a case-insensitive manner, otherwise, False is returned.

# Checking if a substring is contained in a list

If you need to check if a substring is contained in a list, use the any() function.

main.py
my_list = ['bobby123', 'hadz456', 'com789'] my_str = 'bobby' if any(my_str in item for item in my_list): # 👇️ This runs print('The substring is contained in the list') else: print('The substring is NOT contained in the list') # ----------------------------------------------- print(any('bobby' in item for item in my_list)) # 👉️ True print(any('another' in item for item in my_list)) # 👉️ False

The any() function will return True if the substring is contained in a list and False otherwise.

# Checking if a two-dimensional list contains a sublist

You can also use this approach to check if a list contains another list, if you have a two-dimensional list.

main.py
my_list = [['bobby'], ['hadz'], ['com']] print(['hadz'] in my_list) # 👉️ True print(['bobby'] in my_list) # 👉️ True print(['another'] in my_list) # 👉️ False

The expression returns True if the given sublist is contained in the list and False otherwise.

  1. TypeError: 'in <string>' requires string as left operand, not INT
  2. TypeError: 'in <string>' requires string as left operand, not dict

# TypeError: 'in <string>' requires string as left operand, not int

The Python "TypeError: 'in <string>' requires string as left operand, not int" occurs when we use the in operator with an integer and a string.

To solve the error, wrap the integer in quotes, e.g. '5' in my_str.

Here is an example of how the error occurs.

main.py
my_str = '13579' result = 13 in my_str # ⛔️ TypeError: 'in <string>' requires string as left operand, not int print(result)

in string requires string as left operand not int

We used the in operator to check if an integer is contained in a string which caused the error.

# Wrap the integer in quotes when using the in operator

To solve the error, we have to wrap the integer in quotes to make it a string.

main.py
my_str = '13579' result = '13' in my_str print(result) # 👉️ True if '13' in my_str: # 👇️ This runs print('13 is contained in the string') else: print('13 is NOT contained in the string')
Wrapping the integer in quotes solves the error because when the value on the right-hand side is a string, the value on the left-hand side must also be a string.

# Use the str() class to convert the integer to a string

If you have the int stored in a variable, use the str() class to convert it to a string before using the in operator.

main.py
my_str = '13579' my_int = 13 result = str(my_int) in my_str print(result) # 👉️ True if str(my_int) in my_str: # 👇️ This runs print('13 is contained in the string') else: print('13 is NOT contained in the string')

The str() class can be used to convert an object (such as an int) to a string.

Once the values on the left-hand and right-hand sides are of the same type, the error is resolved.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

main.py
my_str = 'num is 13579' print('5' in my_str) # 👉️ True print('0' in my_str) # 👉️ False

The in operator performs a case-insensitive test for whether the substring is contained in the string.

# Check if the string contains a substring in a case-insensitive manner

If you need to ignore the case, convert both strings to lowercase.

main.py
a_str = 'BOBBYHADZ.COM' substring = 'com' if substring.lower() in a_str.lower(): # 👇️ This runs print('The substring is contained in the string') else: print('The substring is NOT contained in the string')

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

Converting both strings to the same case allows for a case-insensitive membership test.

# Checking if a substring is NOT contained in a string

If you need to check if a substring is NOT in a string, use the not in operator instead.

main.py
my_str = 'num is 13579' print('5' not in my_str) # 👉️ False print('0' not in my_str) # 👉️ True

x not in s returns the negation of x in s.

All built-in sequences and set types support the in and not in operators.

Note that empty strings are always considered to be a substring of any other string.

main.py
my_str = '13579' print('' in my_str) # 👉️ True

Checking if an empty string is a substring of any other string will return True.

# TypeError: 'in <string>' requires string as left operand, not dict

The "TypeError: 'in <string>' requires string as left operand, not dict" error is raised when you use the in operator with a dictionary as the left-hand side value.

To solve the error, specify the dictionary on the right-hand side when checking if a key exists.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} # ⛔️ TypeError: 'in <string>' requires string as left operand, not dict if my_dict in 'name': print('The key is contained in the dict')

The code sample causes the error because the dictionary is on the left-hand side of the in operator.

To solve the error, move the dictionary to the right of the operator.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} if 'name' in my_dict: print('The key is contained in the dict') else: print('The key is NOT contained in the dict')
When used with a dictionary, the in operator checks for the existence of the specified key in the dict object.

The in operator will return True if the key exists in the dictionary and False otherwise.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} print('name' in my_dict) # 👉️ True print('another' in my_dict) # 👉️ False

# Checking if a key is not in a Dictionary

If you need to check if a key is not in a dictionary, use the not in operator.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} if 'another' not in my_dict: # 👇️ This runs print('The key is NOT contained in the dict') else: print('The key is contained in the dict') # ---------------------------------------- print('name' not in my_dict) # 👉️ False print('another' not in my_dict) # 👉️ True

The not in operator will return True if the key is not contained in the dictionary and False otherwise.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.