Python map object is not subscriptable -
why following script give error:
payintlist[i] = payintlist[i] + 1000
typeerror: 'map' object not subscriptable
paylist = [] numelements = 0 while true: payvalue = raw_input("enter pay amount: ") numelements = numelements + 1 paylist.append(payvalue) choice = raw_input("do wish continue(y/n)?") if choice == 'n' or choice == 'n': break payintlist = map(int,paylist) in range(numelements): payintlist[i] = payintlist[i] + 1000 print payintlist[i]
in python 3, map
returns iterable object of type map
, , not subscriptible list, allow write map[i]
. force list result, write
payintlist = list(map(int,paylist))
however, in many cases, can write out code way nicer not using indices. example, list comprehensions:
payintlist = [pi + 1000 pi in paylist] pi in payintlist: print(pi)
Comments
Post a Comment