On 2013-07-24, Peter Otten <__pete...@web.de> wrote: >> So, my question boils down to: in Python 3 how is dict.keys() >> different from dict? What are the use cases? > > I just grepped through /usr/lib/python3, and could not identify > a single line where some_object.keys() wasn't either wrapped in > a list (or set, sorted, max) call, or iterated over. > > To me it looks like views are a solution waiting for a problem.
Here's a case of using keys as a set-like view from my own "production" code (i.e., I used it once for one important job): seen = set() students = {} dates = [] for fname in sorted(glob.glob("currentterm201320?.csv")): print(fname, end="\n\t") date = get_date(fname) dates.append(date) term = fname[-11:-4] r = reg.registration(term, path=".") regs = r.keys() for alt_id in regs & seen: students[alt_id].append(r[alt_id]) for alt_id in seen - regs: students[alt_id].append(None) for alt_id in regs - seen: students[alt_id] = [None]*(len(dates)-1) + [r[alt_id]] seen.add(alt_id) It was a very nice way to to do three different things depending on the student sin the set I was working with, compared to a registration list: Granted the line was originally "regs = set(regs.keys())" before it occurred to me that it sucked to take what must be equivalent to a set, convert to a list, and then back to set again. Thanks to the set-like view of dict.keys it worked just like one might hope. Looking at it again "seen" might be a redundant parallel version of students.keys(). -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list