Python Map Object Is Not Subscriptable

Python map object is not subscriptable

In Python 3, map returns an iterable object of type map, and not a subscriptible list, which would allow you to write map[i]. To force a list result, write

payIntList = list(map(int,payList))

However, in many cases, you can write out your code way nicer by not using indices. For example, with list comprehensions:

payIntList = [pi + 1000 for pi in payList]
for pi in payIntList:
print(pi)

map' object is not subscriptable,How to extract a part of map

So I believe that given the information, at some point you may have tried to create a list (edge_list) by mapping a function to another list using map. Please take the following as an example.

lst = [1,2,3]
new_lst = map(lambda x: x**2, lst)
type(new_lst)

This returns a 'map' object which is not iterable. Try casting the new_lst in this example, the edge_list in yours, to a list by:

lst = [1,2,3]
new_lst = list(map(lambda x: x**2, lst))
type(new_lst)

Python 3 - TypeError: 'map' object is not subscriptable

In Python 3 map returns a generator. Try creating a list from it first.

with open("/bin/ls", "rb") as fin: #rb as text file and location 
buf = fin.read()
bytes = list(map(ord, buf))
print (bytes[:10])
saveFile = open('exampleFile.txt', 'w')
saveFile.write(bytes)
saveFile.close()

If you think that is ugly, you can replace it with a list generator:

bytes = [ord(b) for f in buf]


Related Topics



Leave a reply



Submit