“TypeError: 'NoneType' object is not callable” when adding lists to dictionary

前端 未结 3 411
情深已故
情深已故 2021-01-23 20:28

I\'m creating a function that consolidates a couple of lists into a string and am encountering the below error.

Traceback (most recent call last):
  File \"Tradi         


        
相关标签:
3条回答
  • 2021-01-23 20:55

    If you are using Python 2, then either map or dict is bound to None. Check the rest of your code for assignments to either name.

    Note that instead of map(None, iterable1, iterable2) you can use zip(iterable1, iterable2) to get the same output.

    If you are using Python 3, then the map() method does not support None as a first argument:

    >>> list(map(None, [1], [2]))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'NoneType' object is not callable
    

    and you certainly want to use zip() there:

    defsTwo = dict(zip(letters, defsOne))
    
    0 讨论(0)
  • 2021-01-23 21:06

    If you're using Python 3 then first argument to map is ought to be a function. You're passing None so it tries to call a None. This is a different than in Python 2, when None was treated like an identity function (Python's 2 map).

    For Python 2 case see Martijn Pieters's answer.

    0 讨论(0)
  • 2021-01-23 21:16

    #Use Of Zip() function

    #Generate tuple

    x=zip( range(5), range(1,20,2) ) print("Tuple : ",tuple(x))

    #Generate List

    x=zip( range(5), range(1,20,2) ) print("List : ",list(x))

    #Generate dictonary

    x=zip( range(5), range(1,20,2) ) print("Dictonary : ",dict(x))

    0 讨论(0)
提交回复
热议问题