3 ms·
The thing I most dislike about Python is the collection classes aren't sufficiently orthogonal. What do I mean? Python has two main built-in collection types,
by cabalamat 18y ago
The thing I most dislike about Python is the collection classes aren't sufficiently orthogonal.
What do I mean? Python has two main built-in collection types, list and dict. If you have a list you can use "in" to ask whether the list contains a value:
>>> a = ['a','b','c']
>>> 'a' in a
True
>>> 'zzz' in a
False
But if you're using a dict, "in" tells you whether the dict's keys contains a value (not the dict's value).
>>> d = {'x':'a', 'y':'b'}
>>> 'a' in d
False
>>> 'x' in d
True
To enumerate over a dict's key-value pairs, you use .items():
>>> d.items()
[('y', 'b'), ('x', 'a')]
But to enumerate over the key-value pairs in a list, you have to use enumerate():
>>> list(enumerate(a))
[(0, 'a'), (1, 'b'), (2, 'c')]
- anamax 18y ago> Python has two main built-in collection types, list and dict And tuples. > To enumerate over a dict's key-value pairs, you use .items(): You can also use enumerate. > But to enumerate over the key-value pairs in a list, you have to use enumerate(): Just like you can with lists. Python's collection operations do the right thing on collection data types. The fact that those data types also have type-specific operations merely means that those data types have some properties that are not common to all collection types, which is pretty much the reason why they exist.
- anamax 18y agoI forgot that python's sets and strings are also collection types. (IMHO, enumerate's handling of sets is odd, but ....) [And yes, I fumble-fingered "Just like you can with dicts." above.]
- bhiggins 18y agolist and dict are used for completely different reasons and have difference performance. are you perhaps a php programmer?
- deleted 18y ago[deleted]
- cschep 18y agoThis is certainly off-topic, but I love even the insults this community generates. At least he didn't call anyone out for VB...thems fightin' words. Zing!
- bhiggins 18y agophp doesn't have a seperate list vs. hash type. that's what i'm getting at. maybe if you knew something about the language you wouldn't have just thought this was a random insult.