Skip to main content

How to Resolve "TypeError: 'float' object is not subscriptable" in Python

The TypeError: 'float' object is not subscriptable is a common error in Python encountered when attempting to use square bracket indexing ([]) on a floating-point number. Floats, being single numeric values, don't have elements that can be accessed by index.

This guide explains the cause of this error and provides effective solutions.

Understanding the Error: Floats Aren't Subscriptable

"Subscriptable" means an object can be accessed using index notation (square brackets []). Sequences like strings, lists, and tuples, and mappings like dictionaries are subscriptable. Floating-point numbers (float) represent single numeric values and do not have internal elements accessible via an index.

my_float = 3.123456

# This causes the error because floats don't have indices:
# result = my_float[0] # ⛔️ TypeError: 'float' object is not subscriptable

Solution 1: Convert Float to String Before Indexing

To access individual digits or the decimal point within a float, convert the float to a string first. Strings are subscriptable.

my_float = 3.123456
my_float_str = str(my_float) # Convert to string

print(my_float_str[0]) # Output: '3' (first character)
print(my_float_str[2]) # Output: '1' (third character)
print(my_float_str[:3]) # Output: '3.1' (slice up to index 3)

Solution 2: Correcting Variable Assignment

The error might occur if you accidentally assigned a float value to a variable that you intended to be a list, tuple, or string later in your code.

# Incorrect assignment causing the error later
my_list_or_string = 3.14

# ... later in the code ...
# print(my_list_or_string[0]) # ⛔️ TypeError: 'float' object is not subscriptable

Solution: Trace back where the variable was assigned and ensure it holds the correct data type (list, string, etc.) before attempting subscripting.

Handling the Error in Lists of Floats

This error frequently appears when working with lists containing floats.

Accessing Digits of a Float Element

You can not directly access digits within a float element of a list using double indexing like list_of_floats[0][0]. Convert the float element to a string first:

list_of_floats = [1.1, 2.2, 3.3, 4.4]

# Incorrect:
# print(list_of_floats[0][0]) # ⛔️ TypeError

# Correct:
first_element_str = str(list_of_floats[0]) # Convert the float at index 0 to string
print(first_element_str[0]) # Output: '1'

Replacing Multiple Values in a List

If you intended to replace a slice of the list (multiple elements), use list slicing assignment:

list_of_floats = [1.1, 2.2, 3.3, 4.4]
list_of_floats[1:3] = [9.9] * 2 # Replace elements at index 1 and 2
print(list_of_floats) # Output: [1.1, 9.9, 9.9, 4.4]

Updating Individual List Elements

To update specific elements, access them by their list index:

list_of_floats = [1.1, 2.2, 3.3, 4.4]
list_of_floats[1] = 9.9 # Update element at index 1
list_of_floats[2] = 9.9 # Update element at index 2
print(list_of_floats) # Output: [1.1, 9.9, 9.9, 4.4]

Solution 3: Using F-Strings for String Conversion/Formatting

f-strings provide a concise way to convert a float to its string representation before indexing or formatting:

my_float = 3.123456

my_str = f'{my_float}' # Convert to string
print(my_str[0]) # Output: '3'

# Format to 2 decimal places
my_formatted_str = f'{my_float:.2f}'
print(my_formatted_str) # Output: '3.12'

Potential Causes: User Input and Division

  • User Input: The input() function always returns a string. If you expect a number, you must explicitly convert it using float() or int(). Forgetting this conversion can lead to trying to subscript the input string directly.
  • Division Operator (/): In Python 3, the standard division operator (/) always returns a float, even if the result is a whole number (e.g., 10 / 2 is 5.0). If you then try to index this float result, you'll get the error. Use floor division (//) if you need an integer result: 10 // 2 is 10.

What are Subscriptable Objects?

Subscriptable objects are those that support accessing elements using square brackets ([]). They implement the special __getitem__ method. Common subscriptable types in Python include:

  • Sequences: list, tuple, str, bytes, bytearray, range
  • Mappings: dict

Floats and integers are not subscriptable.

Conclusion

The TypeError: 'float' object is not subscriptable clearly indicates an attempt to index a floating-point number.

The primary solution is to convert the float to a string using str() before attempting to access individual characters or slices using square brackets. Also, review your code to ensure variables haven't been unintentionally assigned float values when a subscriptable type like a list or string was expected.