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.

Category:python Views:0 Time:2011-09-04

Related post

  • Python 2 dict_items.sort() in Python 3 2010-03-15

    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

  • What are Python dictionary view objects? 2012-01-21

    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

  • Dictionary acting weird, and how to pack into binary 2010-11-05

    I have the code from struct import pack self.data = {'SYNC' : 0x16, 'SOH' : 0x01, 'FnCode' : 0x55, 'pacingState' : 0, 'pacingMode' : 6, 'hysteresis' : 0, 'hysteresisInterval' : 300, 'lowrateInterval' : 1000, 'vPaceAmp' : 3500, 'vPaceWidth' : 4, 'VRP'

  • Dynamically call a method from a list of Python classes/objects 2012-03-09

    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

  • Generators to iterate over a dictionary uniformly in both Python 2 and 3 2012-05-02

    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

  • How can I merge two Python dictionaries in a single expression? 2008-09-02

    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

  • Py3k memory conservation by returning iterators rather than lists 2009-03-31

    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

  • Python: Does a dict value pointer store its key? 2009-09-21

    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,

  • I am getting this sort of CSV data while making Http request to the CSV file. Very malformed string 2009-11-16

    I am getting this sort of CSV data while making Http request to the CSV file. Very malformed string. response = '"Subject";"Start Date";"Start Time";"End Date";"End Time";"All day event";"Description""Play football";"16/11/2009";"10:00 PM";"16/11/200

  • Map list onto dictionary 2010-01-03

    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

  • In Python, I have a dictionary. How do I change the keys of this dictionary? 2010-02-06

    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

  • List attributes of an object 2010-04-20

    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

  • Creating a spam list with a web crawler in python 2010-05-01

    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

  • Python access to first element in dictionary 2010-06-22

    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

  • Most Efficient way to calculate Frequency of values in a Python list? 2010-07-03

    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

  • Iterating over dictionary items(), values(), keys() in Python 3 2010-09-01

    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

  • non-destructive version of pop() for a dictionary 2010-10-23

    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

  • Python: jQuery-like function chaining? 2010-12-03

    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

  • Pythonic way to avoid a mountain of if-else statements? 2011-04-12

    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

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.090 (s). 11 q(s)