PowerApp101
PowerApp101

Reputation: 1826

Can this be converted to a list comprehension?

The below function returns a list of words from a text file at a URL:

def fetch_words():
    with urlopen('http://sixty-north.com/c/t.txt') as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            for word in line_words:
                story_words.append(word)
    return story_words

I'm interested to see if this can be converted into an equivalent list comprehension. I've tried:

with urlopen('http://sixty-north.com/c/t.txt') as story:
     return [word for word in line.decode('utf-8').split() 
             for line in story]

but it errors with "unresolved reference 'line'". It looks like I'm misunderstanding how nested list comprehensions work, can someone explain?

Upvotes: 0

Views: 94

Answers (3)

cs95
cs95

Reputation: 402413

For simplicity, I recommend using the requests module:

story_words = [
    word 
    for line in requests.get('http://sixty-north.com/c/t.txt').iter_lines() 
    for word in line.decode('utf-8').split()
]

No context manager is required.


If the data you're reading in is reasonably small, you can use

story_words = [
    word 
    for line in requests.get('http://sixty-north.com/c/t.txt').text.splitlines()
    for word in line.split()
]

Upvotes: 2

Ned Batchelder
Ned Batchelder

Reputation: 375574

When a list comprehension has two loops, they have to be in the same order as if they are for statements:

with urlopen('http://sixty-north.com/c/t.txt') as story:
    return [
        word 
        for line in story
        for word in line.decode('utf-8').split() 
    ]

Upvotes: 2

Selcuk
Selcuk

Reputation: 59184

Your syntax is slightly wrong, try this:

return [word for line in story for word in line.decode('utf-8').split()]

Upvotes: 2

Related Questions