Python Forum
Why list(dict.keys()) does not work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why list(dict.keys()) does not work?
#4
(Feb-02-2017, 07:55 AM)wavic Wrote: my_dict = {'a':'aaa','b':'bbb','c':'ccc'}

l = list(my_dict.keys())

print(l)
print(my_dict.keys())

Weird....The script code works. But those in console still does not return "TypeError: 'list' object is not callable". Anyway, I am writing in a script, jsut sometimes do quick testing in console

(Feb-02-2017, 10:06 AM)Ofnuts Wrote:
(Feb-02-2017, 07:19 AM)landlord1984 Wrote:
dict={'a':'aaa','b':'bbb','c':'ccc'} list(dict.keys())
Quote: TypeError Traceback (most recent call last) <ipython-input-60-be97bb392967> in <module>() ----> 1 list(dict.keys()) TypeError: 'list' object is not callable
I want to get a list of the keys of a dictionary for further manipulation. But don't know why this does not work. Somebody here:http://stackoverflow.com/questions/17322...t-indexing suggested they can do by this way, but it does not work for me. Another question is: Can I access dictionary by index instead of key? Thanks, L
That should work. Since you use dict which is a type name as a variable, I can assume that you have a list variable elwhere in your code, that hides the list() function. No, you cannot access a dictionary by index. You can at best access the keys by index and then use the keys to access the values. But the key order can change if you add something to the dictionary.

Ok. I see. I think a need to give up using dictionary in this case. I want to do some manipulation of insert and remove.
Reply


Messages In This Thread
RE: Why list(dict.keys()) does not work? - by wavic - Feb-02-2017, 07:55 AM
RE: Why list(dict.keys()) does not work? - by landlord1984 - Feb-02-2017, 10:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 238 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,442 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 888 May-19-2023, 11:19 AM
Last Post: deanhystad
  How to work with list kafka_trial 8 2,113 Jan-24-2023, 01:30 PM
Last Post: jefsummers
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,271 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,505 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,320 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 1,651 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Remove empty keys in a python list python_student 7 3,181 Jan-12-2022, 10:23 PM
Last Post: python_student
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,183 Jun-26-2021, 11:12 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020