Alex
Alex

Reputation: 4934

Why does this List insert a nested List?

Simple question here; I've got a list of stop words in a CSV text file called stop_words.txt.

I'm using this code to add these to a list:

>>> import csv
>>> stops = []
>>> with open('/stop_words.txt', 'rU') as f:
    reader = csv.reader(f)
    for row in reader:
        stops.append(row)

The problem is that when I run

>>> len(stops)
1

I get length of 1. The contents looks like:

>>> stops
[['a', 'able', 'about', 'across', 'after', 'all', 'almost', 'also', 'am', 'among', 'an', 'and', 'any', 'are', 'as', 'at', 'be', 'because', 'been', 'but', 'by', 'can', 'cannot', 'could', 'dear', 'did', 'do', 'does', 'either', 'else', 'ever', 'every', 'for', 'from', 'get', 'got', 'had', 'has', 'have', 'he', 'her', 'hers', 'him', 'his', 'how', 'however', 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'just', 'least', 'let', 'like', 'likely', 'may', 'me', 'might', 'most', 'must', 'my', 'neither', 'no', 'nor', 'not', 'of', 'off', 'often', 'on', 'only', 'or', 'other', 'our', 'own', 'rather', 'said', 'say', 'says', 'she', 'should', 'since', 'so', 'some', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'tis', 'to', 'too', 'twas', 'us', 'wants', 'was', 'we', 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'whom', 'why', 'will', 'with', 'would', 'yet', 'you', 'your']]

There is a list within a list here, but I don't understand why.

Many thanks.

Upvotes: 1

Views: 121

Answers (4)

Björn Pollex
Björn Pollex

Reputation: 76848

csv.reader returns a list for each row. Since you add row to stops, you add a list to a list. To prevent that, you could use:

stops.extend(row)

Or even better, use a list-comprehension:

stops = [item for row in reader for item in row]

Upvotes: 9

eumiro
eumiro

Reputation: 213005

This looks like your stop_words.txt file is one long line. You can directly use this list:

with open('/stop_words.txt', 'rU') as f:
    stops = next(csv.reader(f))

Upvotes: 2

Dan Breen
Dan Breen

Reputation: 12934

I assume there's only one row in the CSV file, which is a list of all stop words. You're trying to build a list of "rows" where a row is a list. Which is exactly what's happening; there's just the one row. Since there's only one row you can just assign stops to the first row in the csv.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272337

Your csv reader will be splitting your line on the commas, and returning a list. You then add that list (as a single element) to your rows list. Instead iterate through your returned row and add each entry to a list of stop words.

Upvotes: 1

Related Questions