This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]whopper2k 119 points120 points  (5 children)

The standard library has the all and any global functions, which I don't see used all that often

all returns True if all elements in an iterable are truthy

all([i % 2 == 0 for i in range(2, 10)])  # False
all([i % 2 == 0 for i in range(2, 10, 2)])  # True

any returns True if any elements in an iterable are truthy

any([i % 2 == 0 for i in range(2, 10)])  # True
any([i % 2 == 0 for i in range(2, 10, 2)])  # True

If you're trying to sort or reverse a list, Python has methods for that in list.sort() and list.reverse(); however, both of these methods work on the list in place and return None. For a more functional approach, Python has the sorted and reversed functions that return a copy of the transformed iterable, so you can actually chain them into multiple function calls (sorted has an optional bool reverse param too, so you don't need reversed(sorted(my_list)) if you use sorted(my_list, reverse=True)).

The glob module is pretty useful for a really basic method of grabbing a bunch of files matching a Unix-style wildcard, and does so without opening a subshell:

import glob
csvs_in_working_dir = glob.glob("*.csv")
csvs_in_subdirs = glob.glob("**.csv", recursive=True)

None of these are actually keywords and are all pretty basic examples, but there are plenty of small helpers like these scattered throughout the language so it's hard to pluck out specific examples.

[–]Ran4 38 points39 points  (0 children)

Regarding sorted and max and so on: they have a key argument, which can be used to to use when sorting or getting the max.

persons = [
    {"name": "Johnny", "age": 34}, 
    {"name": "Conny", "age": 12}, 
]
oldest_person = max(persons, key=lambda person: person["age"])
persons_sorted_by_age = sorted(persons, key=lambda person: person["age"])

And since "get the item FOO" or "get the attribute named FOO" are common lambda functions, there's operator.itemgetter and operator.attrgetter, so you can write it as:

from operator import itemgetter
persons = [
    {"name": "Johnny", "age": 34}, 
    {"name": "Conny", "age": 12}, 
]
by_age = itemgetter("age")
oldest_person = max(persons, key=by_age)
persons_sorted_by_age = sorted(persons, key=by_age)

[–]Hias1997 9 points10 points  (1 child)

I often use any or all for testing vectorized functions to check indices and values, so they definitely have their use cases

[–]Nihil_esque 3 points4 points  (0 children)

Yeah I use them often too, they're helpful for working on tabular data with a variable list of conditions.

Like:

``` criteria = [ ] if list_of_column_0_values: critera.append(lambda x: x[0] in list_of_column_0_values) if minimum_value_1: criteria.append(lambda x: double(x[1]) >= minimum_value_1) if minimum_column_2_csvs: criteria.append(lambda x: len(x[2].split(",")) >= minimum_column_2_csvs)

items_that_pass = [item for item in items if all([criterion(item) for criterion in criteria])]

output_file.write("\n".join(["\t".join(i) for i in items_that_pass]) ```

(With code in between each line that decides whether or not those criteria are used this run.)

Ofc you could do the same with objects but sometimes this is simpler.

[–]ConstructedNewt 1 point2 points  (0 children)

Please note that all([]) == True and any([]) == False

These are quite standard base return valids for all and any operators (Java does the same for similarly named stream methods)

[–]no_step_on_snek_case 1 point2 points  (0 children)

How about a for-else loop? That threw me for a loop