Reputation: 3
I set the dictionary data type like this.
a={'name':'pey','phone':'0101111','birth':'1111'}
and I want to transform the key to list.
So, I command in iDle like this.
list(a.keys())
I expect this result as
['name','phone','birth']
But, I have error this.
Traceback (most recent call last):File "<pyshell#188>", line 1, in <module> list(a.keys()) TypeError: 'list' object is not callable
How I get the result? ( Key -> list ). Thanks to watch my question.
Upvotes: 0
Views: 185
Reputation: 357
Simply do this. create an empty list lst=[]
and iterate dictionaries keys and append it in list one by one
a={'name':'pey','phone':'0101111','birth':'1111'}
lst=[]
for i in a.keys():
lst.append(i)
print(lst)
Output.
['name','phone','birth']
Upvotes: 2
Reputation: 54
In your code
list(a.keys())
a.keys() was already returning list. So you can just use the following
a.keys()
and BTW list() is not the python way to create a list. Checkout this link for to know more.
Upvotes: 0