How to return dictionary keys as a list in Python?

This article was translated from: How to return dictionary keys as a list in Python?

An In Python 2.7 , the I could GET the Dictionary Keys , values , or items AS A List: In Python 2.7 , I can put the dictionary keys , values , or items acquired as a list:

>>> newdict = {1:0, 2:0, 3:0}
>>> newdict.keys()
[1, 2, 3]

Now, in Python> = 3.3 , I get something like this: Now, in Python> = 3.3 , I get the following information:

>>> newdict.keys()
dict_keys([1, 2, 3])

So, I have to do this to get a list: Therefore , I have to do this to get the list:

newlist = list()
for i in newdict.keys():
    newlist.append(i)

'M wondering the I, there IS A return of Better Way to A List in Python 3 ? I want to know, is there a better way in Python 3 Back to list?


#1st Floor

Reference: https://stackoom.com/question/18ZRm/ How to return dictionary keys as a list in Python


#2nd Floor

Try list(newdict.keys()). Try list(newdict.keys()).

By Will at The Convert For the this dict_keysObject to A List. This will dict_keysobject into a list.

On the other hand, you should ask yourself whether or not it matters. On the other hand, you should ask yourself whether it is important. At The Pythonic Way to code IS to the ASSUME Duck Typing ( IF IT looks like A Duck and IT quacks like A Duck, IT's A Duck ). Python encoding is assumed that duck input ( if it looks like a duck, and quack like a duck call , That's the duck ). At The dict_keysObject by Will ACT like A List for MOST Purposes. In the dict_keysrole of the object is similar to the list. For instance: For example:

for key in newdict.keys():
  print(key)

Obviously, insertion operators may not work, but that doesn't make much sense for a list of dictionary keys anyway. Obviously, insertion operators may not work, but for dictionary keyword lists, this doesn't make much sense.


#3rd floor

The 'bit ON OFF A "Duck Typing" Definition - dict.keys()Returns AN Iterable Object, List A Not-like Object. On the "duck Type" is defined dict.keys()departing dict.keys()returns an iterator objects, rather than a list-like object. It will work anywhere an iterable will work-not any place a list will. It can be used anywhere iterable -the list cannot be used anywhere. a list is also an iterable, but an iterable is NOT a list (or sequence ...) list is also iterable, but iterable is not a list (or sequence ...)

In real use-cases, the most common thing to do with the keys in a dict is to iterate through them, so this makes sense. In actual use cases, the most common thing related to keys in a dictionary is to traverse them, So it makes sense. And if you do need them as a list you can call list(). If you really need them as a list, you can call them list().

Similarly for Very zip()of tuples the Just the throw to the then IT Away Again Why the Create new new AN Entire List and iterate through IT -? - in at The VAST Majority of Cases, IT IS Iterated through with zip()very similar - under most circumstances, it will Iterated-why did you create a new list of entire tuples just to iterate and then discard it?

This is part of a large trend in python to use more iterators (and generators), rather than copies of lists all over the place. This is to use more iterators (and generators) in Python instead of using list copies everywhere Part of a megatrend.

dict.keys()should work with comprehensions, though - check carefully for typos or something ... it works fine for me: dict.keys()It should be understood - Check carefully for spelling ... for me to good use:

>>> d = dict(zip(['Sounder V Depth, F', 'Vessel Latitude, Degrees-Minutes'], [None, None]))
>>> [key.split(", ") for key in d.keys()]
[['Sounder V Depth', 'F'], ['Vessel Latitude', 'Degrees-Minutes']]

#4th floor

list(newdict)Works in both Python 2 and Python 3, providing a simple list of the keys in newdict. Both list(newdict)can be used newdict in Python 2 and Python 3, providing a simple list of the keys in . keys()isn't necessary. keys()Not necessary. (: (:


#5th Floor

A CAN Also use by You List comprehensions : You can also use list comprehensions :

>>> newdict = {1:0, 2:0, 3:0}
>>> [k  for  k in  newdict.keys()]
[1, 2, 3]

Or, shorter, or shorter

>>> [k  for  k in  newdict]
[1, 2, 3]

Note: Order is not guaranteed on versions under 3.7 (ordering is still only an implementation detail with CPython 3.6). Note: In versions below 3.7, ordering is not guaranteed (ordering is still only the implementation details of CPython 3.6).


#6th floor

A List to the without a using Converting at The keysMethod, Makes More readable IT: without using keysthe conversion method in the case of the list make it more readable:

list(newdict)

and, when looping through dictionaries, there's no need for keys(): and, when looping through dictionaries, there is no need to :keys()

for key in newdict:
    print key

Unless you are modifying it within the loop which would require a list of keys created beforehand: unless you want to modify it in the loop, you will need a pre-created key list:

for key in list(newdict):
    del newdict[key]

2 there the Python A IS ON Marginal Performance the using GAINkeys() . On Python 2, used keys()to obtain a small amount of performance .

Published 0 original articles · praised 8 · 30,000+ views

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/105554995