Select Page

How to Solve Python TypeError: ‘map’ object is not subscriptable

by | Programming, Python, Tips

In Python 3, a map object is an iterator and is not subscriptable. If you try to access items inside a map object using the subscript operator [], you will raise the TypeError: ‘map’ object is not subscriptable.

This error typically occurs when using Python 2 syntax when using Python 3. In Python 2, calling the built-in map() function returns a list, which is subscriptable.

You can solve this error by converting the map object to a list using the built-in list function. For example,

new_list = list(map(int, old_list))

You can also iterate over the values in the iterator using a for loop

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: ‘map’ object is not subscriptable

A TypeError occurs when you perform an illegal operation for a specific data type.

What Does Subscriptable Mean?

The subscript operator, which is square brackets [], retrieves items from subscriptable objects like lists or tuples. The operator calls the __getitem__ method, for example, a[i] is equivalent to a.__getitem__(i).

All subscriptable objects have a __getitem__ method. Map objects are iterators and do not have a __getitem__ method. We can verify that map objects do not have the __getitem__ method by defining a creating a map object and passing it to the dir() method:

def square(i):
    res = i ** 2
    return res

lst = [2, 3, 4, 5]

squared = map(square, lst)

print(dir(squared))
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

 A Python iterator object must implement two special methods, __iter__() and __next__(), collectively called the iterator protocol.

Example

This error typically occurs when using Python 2 map operations in Python 3. First, let’s verify what the built-in map function returns in Python 2. We will use a virtual environment with Python 2 installed and confirm that we are using Python 2 using the sys library:

import sys

print(sys.version)
2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

Next, we will define a list of strings, which we will convert to a list of integers using the map() function. We will pass the object returned by the map function to the type() function to get the type.

string_list = ["2", "3", "4", "5", "6"]

int_list = map(int, string_list)

print(type(int_list))

Let’s run the code to get the result:

<type 'list'>

In Python 2, the map function returns a list, which is subscriptable. We access an element of the list using the subscript operator, for example:

print(int_list[0])
2

Let’s try to do this using Python 3. We will use a different virtual environment with Python 3 installed, which we will verify using sys.

import sys

print(sys.version)
3.8.12 (default, Oct 12 2021, 06:23:56) 
[Clang 10.0.0 ]

Next, we will define our list of strings and use the map() function to convert it to a list of integers. We will pass the object returned by the map function to the type function.

string_list = ["2", "3", "4", "5", "6"]

int_list = map(int, string_list)

print(type(int_list))

Let’s run the code to see the result:

<class 'map'>

In Python 3, the map function returns a map object, which is an iterator. If we try to access an element of the map object using the subscript operator, we will raise the TypeError.

print(int_list[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 print(int_list[0])

TypeError: 'map' object is not subscriptable

Solution #1

We can convert map objects to lists using the built-in list() function. Let’s look at the revised code:

string_list = ["2", "3", "4", "5", "6"]

int_list = list(map(int, string_list))

print(type(int_list))

print(int_list[0])

Let’s run the code to get the result:

<class 'list'>
2

Solution #2

We can access values in an iterator using a for loop, which invokes the __next__() method of the map iterator. Let’s look at the revised code:

string_list = ["2", "3", "4", "5", "6"]
int_list = map(int, string_list)

for i in int_list:
    print(i)

Let’s run the code to get the result:

2
3
4
5
6

Summary

Congratulations on reading to the end of this tutorial! The TypeError: ‘map’ object is not subscriptable occurs when you try to access an item from a map object in Python 3. The Python 3 map object is an iterator and is not subscriptable. We can convert map objects to lists which are subscriptable.

For further reading on TypeErrors, go to the article: How to Solve Python TypeError: ‘function’ object is not subscriptable

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!