What questions should every good Python developer be able to answer?
Alex Martelli, author of the Python Cookbook and Python guru extraordinaire, gave these four questions:
• how do you sort a list of dicts by the value each has for key 'foo'?
There's a really nice page on sorting here. Recall that the built-in function
sorted
takes a keyword argument key
. I usually define a function to feed to sorted
,but the cool kids like lambdas:
sorted also can do reverse:
• how do you get the highest 10 integers out of a list of a million integers?
The built-in function sort can do this in an instant.
• how do you sort a list of strings in case-insensitive alphabetical order?
Feed the built-in function
str.lower
to sort
• given a list of ints, how do you make a string with their str forms separated by spaces?
This one's trivial.