On May 26, 5:33 pm, Daniel Kluev <dan.kl...@gmail.com> wrote: > On Wed, May 25, 2011 at 3:10 AM, Octavian Rasnita <orasn...@gmail.com> wrote: > >> Once again. Suppose we have array of key-value pairs (two-dimensional > >> array), > > > This is a forced example to fit the way Python can do it with a clean > > syntax, but I don't think there are cases in which somebody wants to create > > hashes/dictionaries where the key is not a plain string but an array. > > > This is not a rare case, but a case that probably nobody needs, ever. > > This is far more popular case than converting flat lists into dicts in > Python world. In fact, I *never* had need to convert flat list instead > of properly structured one. Thats why we have both lists and tuples, > after all.
I agree that it's almost never needed to convert flat lists. I've used python for over 10 years and I remember exactly one time when I needed to do that. It turned out that it's a bit tricky and hacky to do in python, in the sense that it's hard to remember if I'll ever need it again, but it will only take minutes to google it. For example, in one piece of code I did recently I created a dict of range tuples and counts from a sequential list, like so: ranges = [(x*width, (x+1)*width) for x in range(y)] data = dict((x,0) for x in ranges) A perl programmer would instead create a flat list and then convert it to dict. And if he were new to python he'd create a flat list and then be annoyed that there's no quick and easy way to make it into a dict. Python way in this case is more structured and disciplined, and the only "flaw" I can see is that it doesn't meet expectations of perl programmers. -Rainy -- http://mail.python.org/mailman/listinfo/python-list