why in returns values for array and keys for dictionary

I

++imanshu

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
 
A

alex23

    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...
{'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?
 
A

alex23

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.
 
D

Dan Bishop

    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.
 
M

Marc 'BlackJack' Rintsch

    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
 
A

alex23

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 :)
 
A

Asun Friere

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?
 
B

bearophileHUGS

"++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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top