Cha Minjae
Cha Minjae

Reputation: 3

Transform the keys to list, I got a problem type error in dictionary data type

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

Answers (2)

kaleem231
kaleem231

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

Jino Jossy
Jino Jossy

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

Related Questions