3 ms·
> 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 e
by 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.]