Re: why in returns values for array and keys for dictionary
On Aug 26, 10:49 am, ++imanshu [EMAIL PROTECTED] wrote: Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. NO! When you iterate over a list (or even a array) it is the members of the list in the order they appear that is of interest. When you iterate over a dictionary it is the relationship between the (unique) key and the (possibly non-unique) value that is of interest. Moreover the sequence of values in a dictionary lacks meaning. What is the 'key' of a list? It's index? It would be cumbersome to iterate over the range(len(list)) and then have to use the index values to pull out the values from that list. On the otherhand it would be useless for 'in' (in the sense of for x in {...}) to return a series of unordered values, with no way to get at the key, rather than keys (from which the values are directly accessible). And what would you like file_like objects, for example, to return? -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
Marc 'BlackJack' Rintsch wrote: On Mon, 25 Aug 2008 19:57:06 -0700, alex23 wrote: On Aug 26, 10:49 am, ++imanshu [EMAIL PROTECTED] wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. […] In both cases, 'in' returns a boolean indicating the existence of an item in the list, or a key in the dict. I'm not sure why you'd need it to return the item you're checking for the existence of, as you'd have to have that item before you could do the check. Have I missed what you're asking for here? Could you provide a pseudocode example to demonstrate what you mean? The OP isn't talking about the ``in`` operator but ``in`` as part of ``for … in …``. So it's actually the question why ``list(a_dict)`` doesn't return a list of values but a list of keys. That is basically the same question. Iterating over a list gives you its elements, and using the `in` operator with lists tells you whether or not an object is an element of the list. Iterating over a dictionary gives you its _keys_, not its values, and the `in` operator with dictionaries tells you whether or not a _key_ is in the dictionary. l = [1, 2, 3] for x in l: print x ... 1 2 3 0 in l False 1 in l True d = {'a': 1, 'b': 2, 'c': 3} for x in d: print x ... a c b 'a' in d True 1 in d False The reason why people find it more useful to deal with keys rather than values of a dictionary during iteration or containment testing is ... because that tends to be what you're usually more interested in, and is more efficient. For another thing, if you're doing a lot of testing for containment in values, then it's likely you're not using the right data structure, or combination of data structures. That's not what dictionaries are for. -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 18 N 121 57 W AIM, Y!M erikmaxfrancis Well I have been puppetized / Oh how I have compromised -- Lamya -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
++imanshu wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. It's because dealing with keys makes far more sense, since that's how the dictionary data structure works. If you're doing this an awful lot -- whether testing for inclusion or iterating -- then you're probably using the wrong data structure. -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 18 N 121 57 W AIM, Y!M erikmaxfrancis Well I have been puppetized / Oh how I have compromised -- Lamya -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Aug 26, 11:52 am, Erik Max Francis [EMAIL PROTECTED] wrote: ++imanshu wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. It's because dealing with keys makes far more sense, since that's how the dictionary data structure works. If you're doing this an awful lot -- whether testing for inclusion or iterating -- then you're probably using the wrong data structure. Thanks for the detailed replies. Python seems to be taking the pragmatic approach here instead of the pure one. And yes it makes more sense. Thanks, ++imanshu -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 18 N 121 57 W AIM, Y!M erikmaxfrancis Well I have been puppetized / Oh how I have compromised -- Lamya -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
++imanshu: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. D language works like you say, and it's awful. With a key you can find its value, but given only the value you can't find its key. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Aug 26, 4:04 pm, ++imanshu [EMAIL PROTECTED] wrote: On Aug 26, 11:52 am, Erik Max Francis [EMAIL PROTECTED] wrote: ++imanshu wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. It's because dealing with keys makes far more sense, since that's how the dictionary data structure works. If you're doing this an awful lot -- whether testing for inclusion or iterating -- then you're probably using the wrong data structure. Thanks for the detailed replies. Python seems to be taking the pragmatic approach here instead of the pure one. And yes it makes more sense. Anyway, there is two obvious choice when dealing with dictionary looping: return the keys and return the key and value. The python designer thought that it is trivial to get the value from key so returning both key and value, when the value might not be used at all, is redundant and might cause a slow down. In the case where it is actually needed for practicality (usually one-liner), then dict.iteritems() is provided, in other cases where you don't need the key (usually when you're applying a uniform function to dictionary or to convert it to a list), dict.itervalues() is provided, and for completeness dict.iterkeys() which is completely useless in a for-loop is provided (the only case where it _might_ be useful is in callbacks). Anyway, in short (in code): for key in dict: print key, dict[key] is exactly the same as: for key, value in dict.iteritems(): print key, value or for key in dict: value = dict[key] print key, value # the next line is not needed if you don't modify an immutable value dict[key] = value -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
Lie wrote: Anyway, there is two obvious choice when dealing with dictionary looping: return the keys and return the key and value. The python designer thought... The issue of whether there should be a default iterator and if so, which of the two obvious choices should be picked, was discussed on the py-dev list by several developers in addition to GvR. 'Yes, iterkeys' was the majority choice as the most useful for the most users. The rationale for no default would have been 'explicit is better than implicit' and the easy prediction (proven true ;-) that either choice would be challenged. Of course, no default would have been criticized also. tjr -- http://mail.python.org/mailman/listinfo/python-list
why in returns values for array and keys for dictionary
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks, ++imanshu -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Aug 26, 10:49 am, ++imanshu [EMAIL PROTECTED] wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. By 'arrays' do you mean lists? tuples? I'm not sure how you'd ever find lists dictionaries similar... alist [1, 2, 3] adict {'a': 1, 'c': 3, 'b': 2} One is a sequence, with convenience functions for treating it like a stack or a queue. The other is a mapping between keys and pairs. In both cases, 'in' returns a boolean indicating the existence of an item in the list, or a key in the dict. I'm not sure why you'd need it to return the item you're checking for the existence of, as you'd have to have that item before you could do the check. Have I missed what you're asking for here? Could you provide a pseudocode example to demonstrate what you mean? -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Aug 26, 12:57 pm, alex23 [EMAIL PROTECTED] wrote: By 'arrays' do you mean lists? tuples? My apologies, there actually -is- an array type in Python. I've just honestly never had any cause to use it :) I'm still not entirely sure what you would like 'in' to do, though. -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Aug 25, 9:57 pm, alex23 [EMAIL PROTECTED] wrote: On Aug 26, 10:49 am, ++imanshu [EMAIL PROTECTED] wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. By 'arrays' do you mean lists? tuples? I'm not sure how you'd ever find lists dictionaries similar... alist [1, 2, 3] adict {'a': 1, 'c': 3, 'b': 2} One is a sequence, with convenience functions for treating it like a stack or a queue. The other is a mapping between keys and pairs. You could argue that lists are also a mapping between keys and pairs, with the constraint that the keys have to be the integers from 0 to len(x)-1. That is, ['a', 'b', 'c'] is like {0: 'a', 1: 'b', 2: 'c'}, at least as far as the [] operator and the len function are concerned. -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Mon, 25 Aug 2008 19:57:06 -0700, alex23 wrote: On Aug 26, 10:49 am, ++imanshu [EMAIL PROTECTED] wrote: Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. […] In both cases, 'in' returns a boolean indicating the existence of an item in the list, or a key in the dict. I'm not sure why you'd need it to return the item you're checking for the existence of, as you'd have to have that item before you could do the check. Have I missed what you're asking for here? Could you provide a pseudocode example to demonstrate what you mean? The OP isn't talking about the ``in`` operator but ``in`` as part of ``for … in …``. So it's actually the question why ``list(a_dict)`` doesn't return a list of values but a list of keys. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: why in returns values for array and keys for dictionary
On Aug 26, 2:30 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: The OP isn't talking about the ``in`` operator but ``in`` as part of ``for … in …``. So it's actually the question why ``list(a_dict)`` doesn't return a list of values but a list of keys. Aaaah! Cheers, Marc, that didn't occur to me. imanshu: my apologies for the confusion in my original replies, I now understand what you mean. I'd hazard to suggest the answer is one of practicality over purity between object types. I can actually see there being a perceived advantage in providing a consistent interface, so that: for index,value in array('i',[0,1,2]): ... for index,value in [0,1,2]: ... for key,value in {'0':0,'1':1,'2':2}: ... would all produce equivalent results. I'd guess that what is provided now meets the most common use cases, with convenience functions readily available for the next most common, ie via enumerate() for arrays lists, and .itervalues() or .iteritems() for dicts. It all comes down to the intent of the data types used. I'm - generally- more concerned with the values of a list than knowing their position, especially when the position is implicit in the order returned. When working with dicts, if I need to modify their contents I -need- to know the key, as the key isn't an implicit aspect of the return order of the values, and is the only way distinguish between identical values. I'm -still- embarrassed that I missed the existence of the array type, however :) -- http://mail.python.org/mailman/listinfo/python-list