Tsviki Hirsh wrote:

 >>>dict.fromkeys('at',50)
{'a': 50, 't': 50}

This is obviously not what I wanted.

Not obvious at all. It is precisely what you asked for ;-)

Please read the doc:
5.8. Mapping Types — dict
"classmethod fromkeys(seq[, value])
Create a new dictionary with keys from seq and values set to value."

The purpose of .fromkeys is to create a dict with multiple keys initially mapped to the one and same value (None by default). If you do not want that, do not use it.

or nearly as clear:
>>> help(dict.fromkeys)
fromkeys(...)
dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None.

I *strongly* recommend that all beginners peruse the Library Reference up through Chapter 5. It you are puzzled by the behavior of a builtin object, go back and look up the specific doc.

What are the alternatives?

If you want {'at':50}, say that.

Terry Jan Reedy



--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to