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?
#2
Hello!
First, do not use keywords as variable names. I mean dict.

You can call list() built-in in the interpreter but in a script, you have to assign list() returns to a variable or use it as a callback.

The interpreter:
In [1]: dict={'a':'aaa','b':'bbb','c':'ccc'}

In [2]: list(dict.keys())
Out[2]: ['b', 'a', 'c']
In script:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

my_dict = {'a':'aaa','b':'bbb','c':'ccc'}

l = list(my_dict.keys())

print(l)
print(my_dict.keys())
Output:
Output:
['a', 'c', 'b'] ['a', 'c', 'b']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 287 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,475 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 922 May-19-2023, 11:19 AM
Last Post: deanhystad
  How to work with list kafka_trial 8 2,142 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,297 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,531 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,348 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 1,671 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Remove empty keys in a python list python_student 7 3,244 Jan-12-2022, 10:23 PM
Last Post: python_student
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,215 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