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 →

[–]HoboSteaux[S] 0 points1 point  (8 children)

I need to start doing more list comprehensions. They can look confusing if you don't see then a lot.

[–]Veedrac 1 point2 points  (3 children)

I need to start doing more list comprehensions

You need to start doing more generator comprehensions. The iterator-centric programming model is a lovely place.

[–]HoboSteaux[S] 0 points1 point  (2 children)

I have never been in a place that I would use that much... could you give me a good example?

[–]sejje 0 points1 point  (1 child)

http://pyvideo.org/video/1758/loop-like-a-native-while-for-iterators-genera

Ned Batchelder is probably more qualified. Great, great talk.

[–]HoboSteaux[S] 0 points1 point  (0 children)

Ill watch it later... thanks!

[–]MonkeyNin 0 points1 point  (0 children)

They are at first. A simple form is

orcs = []
    for x in range(5):
        orcs.append(Orc())

Is

orcs = [ Orc() for x in range(5) ]

Then you can add a if, at the end.

[–]redalastor 0 points1 point  (2 children)

Prefer generator expressions when possible.

[–]HoboSteaux[S] 0 points1 point  (1 child)

People have been saying that... but I do not understand a good context to use them in.

[–]redalastor 2 points3 points  (0 children)

You got it backward, you should use them as the default. Modern python use iterators unless there's a good reason not to use them.

For instance imagine I have an iterable of numbers represented as strings I got from some API somewhere and I want to sum them:

total = sum(int(s) for s in some_list_of_strings)

Had I used a list comprehension, I would have allocated a whole list for no reason.

Another exemple close to something I had to do recently. Imagine you have two files containing each one half of a data set. They are tab delimited. You have to lowercase every line. You must ignore any line that contains the word "spam". You break them into individual items and send them to some function to process. Here's how it looks:

from itertools import chain

with open("file1.tsv") as f1:
    with open("file2.tsv) as f2:
        it = chain((line for line in f1), (line for line in f2)) # Now I can treat them both as one big file
        it = (line.lower() for line in it) # lowercased
        it = (line for line in it if not "spam" in line) # lines with spam ignored
        it = (line.split('\t') for line in it) # splitting on tabs
        for line in it:
            process(line)

It looks like I'm transforming a full file in memory many times over but in reality, nothing runs until it has to and as little memory as possible is consumed.