473,378 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

dict.keys() ?

The PEP 3100:
http://www.python.org/dev/peps/pep-3100/
says:

Return iterators instead of lists where appropriate for atomic type
methods (e.g. dict.keys(), dict.values(), dict.items(), etc.); iter*
methods will be removed. Better: make keys(), etc. return views ala
Java collections???
....
To be removed:
dict.setdefault()? [15]
dict.has_key() method [done]
I may be sleepy now, but maybe the keys() method too can be removed;
otherwise the following two become really the same:

print list(adict)
for k in adict: ...

print list(adict.keys())
for k in adict.keys(): ...

Bye,
bearophile

Jan 27 '07 #1
1 2264
On Jan 26, 10:04 pm, bearophileH...@lycos.com wrote:
The PEP 3100:http://www.python.org/dev/peps/pep-3100/
says:

Return iterators instead of lists where appropriate for atomic type
methods (e.g. dict.keys(), dict.values(), dict.items(), etc.); iter*
methods will be removed. Better: make keys(), etc. return views ala
Java collections???
...
To be removed:
dict.setdefault()? [15]
dict.has_key() method [done]

I may be sleepy now, but maybe the keys() method too can be removed;
otherwise the following two become really the same:

print list(adict)
for k in adict: ...

print list(adict.keys())
for k in adict.keys(): ...
Alas, the consensus has moved away from simple iterators to less
lightweight "views":

http://svn.python.org/view/peps/trun...96&view=markup

"The objects returned by the .keys() and .items() methods behave like
sets with limited mutability; the allow removing elements, but not
adding them.(...) The object returned by the values() method behaves
like a multiset."

I for one am not convinced that adding three new types is worth the
complexity and the error-proneness of having two new
similar-but-not-quite-the-same APIs with sets. Not only iteration is
arguably the most common operation on a view, but the cost (in extra
keystrokes and runtime performance) of populating any container that
the user may need from an iterator is pretty low.

George

Jan 27 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
6
by: David Rasmussen | last post by:
If I have a collection of dicts like: john = {'id': 1, 'name': "John Cleese", 'year': 1939} graham = {'id': 2, 'name': "Graham Chapman", 'year': 1941} I could store all of them in a list. But...
10
by: Gregory Piñero | last post by:
Hey guys, I thought I'd throw this out there since everyone loves optimization questions (it's true, check out the number of replies to those type of questions!) Right now I have a function...
6
by: Kamilche | last post by:
I have a code snippet here that prints a dict in an arbitrary order. (Certain keys first, with rest appearing in sorted order). I didn't want to subclass dict, that's error-prone, and overkill for...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
3
by: aking | last post by:
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest value and then extract the key for that...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
3
by: james_027 | last post by:
hi, a_dict = {'name':'apple', 'color':'red', 'texture':'smooth', 'shape':'sphere'} is there any difference between .. for key in a_dict: from
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
12
by: Florian Brucker | last post by:
Hi everybody! Given a dictionary, I want to create a clustered version of it, collecting keys that have the same value: {1:, 2:, 3:} That is, generate a new dict which holds for each value...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.