what is dict_keys, dict_items and dict_values

I came across these three types when I used collections.Counter's viewkeys(),viewitems() and viewvalues() method.

The value those three methods returned are of type dict_keys, dict_items and dict_values.

They are iterable, as I have noticed.

But my question is:

why do these three types exist? Or what's their usage?

-------------Problems Reply------------

The What's new in 2.7 document is one place these are introduced. These "views" were introduced (proposed here) for Python 3 (and backported to 2.7, as you've seen) to serve as a best-of-all-worlds for the pieces of the dict they refer to.

Before we had the keys/values/items methods which simply made lists. This wastes memory by copying the dict's information and we had the iterkeys/itervalues/iteritems methods that didn't waste this memory but weren't very featureful (the only thing you could do is iterate over them, and you could only do so once). These new views have logical features, such as set operations, efficient comparison, and being iterable multiple times.

Related post

I'm porting some code from Python 2 to 3. This is valid code in Python 2 syntax: def print_sorted_dictionary(dictionary): items=dictionary.items() items.sort() In Python 3, the dict_items have no method 'sort' - how can I make a workaround for this i

In python 2.7, we got the dictionary view methods available. Now, I know the pro and cons of the following: dict.items() (and values, keys): returns a list, so you can actually store the result dict.iteritems() (and the like): returns a generator, so

I have X number of python classes that all inherit from the same abstract base class, which expects children to implement a single method called executeTest() Class TestCase: def executeTest(): #do some logic for the test, then return if it passed or

Is there a way to efficiently iterate over the values/items in a dictionary that works in both Python 2 and Python 3? In Python 2, I can write for x in mydict: for x in mydict.iterkeys(): for x in mydict.viewkeys(): for x in mydict.itervalues(): for

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update() method would be what I need, if it returned its result instead of modifying a dict in-place. >>> x = {'a':1, 'b

Many methods that used to return lists in Python 2.x now seem to return iterators in Py3k Are iterators also generator expressions? Lazy evaluation? Thus, with this the memory footprint of python is going to reduce drastically. Isn't it? What about f

I'm wondering if there is a built-in way to do this... Take this simple code for example: D = {'one': objectA(), 'two': objectB(), 'three': objectC()} object_a = D['one'] I believe object_a is just pointing at the objectA() created on the first line,

Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example; somefunction(lambda a: a[0], ["hello", "world"]) => {"h":"hello", "w

Let's say I have a pretty complex dictionary. {'fruit':'orange','colors':{'dark':4,'light':5}} Anyway, my objective is to scan every key in this complex multi-level dictionary. Then, append "abc" to the end of each key. So that it will be: {'fruitabc

Is there a way to grab a list of attributes that exist on instances of a class? (This class is just an bland example, it is not my task at hand.) class new_class(): def __init__(self, number): self.multi = int(number) * 2 self.str = str(number) a = n

Hey guys, I'm not trying to do anything malicious here, I just need to do some homework. I'm a fairly new programmer, I'm using python 3.0, and I having difficulty using recursion for problem-solving. I've been stuck on this question for quite a whil

If dict is not empty, the way I use to access first element in dict is: dict[dict.keys()[0]] Is there any better way to do this? --------------Solutions------------- Non-destructively you can: dict.itervalues().next() On Python 3 this becomes next (i

I am looking for a fast and efficient way to calculate the frequency of list items in python: list = ['a','b','a','b', ......] I want a frequency counter which would give me an output like this: [ ('a', 10),('b', 8) ...] The items should be arranged

If I understand correctly, in Python 2, iter(d.keys()) was the same as d.iterkeys(). But now, d.keys() is a view, which is in between the list and the iterator. What's the difference between a view and an iterator? In other words, in Python 3, what's

Is there any idiom for getting an arbitrary key, value pair from a dictionary without removing them? (P3K) EDIT: Sorry for the confusing wording. I used the word arbitrary in the sense that I don't care about what I'm getting. It's different from ran

I couldn't find anything on this subject on Google, so I think I should ask it here: Is it possible to chain functions with Python, like jQuery does? ['my', 'list'].foo1(arg1, arg2).foo2(arg1, arg2).foo3(arg1, arg2) #etc... I am losing a lot of space

This has come up several times recently and I'd like to deal with it better than I have been: I have a series of attributes that I'm cross referencing between an object and a dictionary. If the value is different between them, I want to set the objec