Home > Back-end >  Convert dictionary value into list does not work in a function
Convert dictionary value into list does not work in a function

Time:10-13

This works without in a function.

v = list(dic.values()) #  V is a list.

But if I put it in a function, for example:

def test(list):
    result={}
    for i in l:
        result[i]=result.get(i,0) 1
    v = result.values()
    return list(v)
l=[1,2,1,1,3,3,5,5,5,5,5,3,7]
print(test(l))

it raise error,

Traceback (most recent call last):
  File `repNone.py`, line 83, in <module>
    print(test(l))
  File "`repNone.py`", line 81, in test
    return list(v)
TypeError: 'list' object is not callable

CodePudding user response:

Always don't use Built-in Function (like list) as variables, You use list as variable

In your function you pass l as variable with name list then when you use list(v) you got an error because you can not call variable.

you did this:

>>> def test(list):
        ...
...     return list(v) # <- got error because here list is variable and you call this.

change your code like this:

>>> def test(lst):
        ...
...     return list(v)
  • Related