-5

I created a dict like so: dic = {80:90, 7:60, 9:1} then I converted the dict into a list li = [dic.keys()] but when I print I get [dict_keys(['80, 7, 9'])].

How do I remove everything but '80, 7, 9'?

Konrad Talik
  • 912
  • 8
  • 22
Grimzy
  • 1
  • try `li = list(dic.keys())` if you want the keys as a list (as opposed to a 1-element list whose sole item is an iterable) – John Coleman Jun 05 '16 at 23:05
  • The described rationale for closing seems wrong -- the problem seems more one of not being able to get the keys into a list (rather than in not being able to print the list, though the latter seems like it is likely a problem for them as well). – John Coleman Jun 05 '16 at 23:12
  • 1. Please indicate which Python version you're reffering to. From mentioned output I assume your question is related to Python 3. 2. Also the final question is not well stated. You can remove everything but `'80, 7, 9'` parsing the final output or changing the `li` conversion code. Assuming the latter and a **conversion** keyword, the answer is to use `list` constructor directly to perform the conversion: `li = list(dic.keys())`. Reason? `[]` brackets put an item into the list, that's why you have a nested list here. See also Python 2, where your `li` is displayed as `[[80, 9, 7]]`. – Konrad Talik Jun 05 '16 at 23:17

1 Answers1

0

Try this

li = dic.keys()

keys() already returns a list of the keys in your dictionary.

prithajnath
  • 2,000
  • 14
  • 17
  • 1
    The question clearly involves Python 3 (based on the output that they describe). Your answer is only true of Python 2. Python 3 is much more lazy -- it only produces a list in a case like this if you force it to by using `list()` – John Coleman Jun 05 '16 at 23:09
  • No wonder I despise Python 3. Thanks though. – prithajnath Jun 05 '16 at 23:27