Maxine Mou
Maxine Mou

Reputation: 23

list.append in python 3: nonetype is not iterable?

I am trying to add every word in romeo.txt to an empty list.
I thought this code was okay but python3 returns a traceback saying

File "test.py", line 13, in <module> if i in lst: TypeError: argument of type 'NoneType' is not iterable

here's my code:

fh = open("romeo.txt")
lst = list()
words = fh.read()
list1 = words.split()
for i in list1:
    if i in lst:
        continue
    else:
        lst = lst.append(i)
lst = lst.sort()
print(lst)

Upvotes: 2

Views: 2034

Answers (4)

idjaw
idjaw

Reputation: 26570

Explanation

You are making the same mistake twice. The first place you are making this error and the reason why you are getting the current error you are seeing is because of this:

lst = lst.append(i)

The append method actually performs the append in place. The actual return of that call is going to be None, because it doesn't return anything. It doesn't need to, since it does the work on the list.

So, to narrow down exactly what is going on. When you did lst = lst.append(i). lst will now hold None. So, the next time it goes through its loop and you get here:

if i in lst:

You are checking if i is in None. Since now lst will hold None, it is exactly here where your error message gets raised and you are seeing that Traceback.

To replicate:

   >>> 'a' in None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'NoneType' is not iterable

Solution

To solve this, you just need perform the append like:

lst.append(i)

Second Mistake

You are also making the same mistake when calling sort here:

lst = lst.sort()

It is again a method that performs its work in place, therefore you just need:

lst.sort()

Extra Note

Don't forget to close your file at the end of your code (or when you are done using the file:

fh.close()

Ideally, it is always best to use a context manager as explained in the other answer here. Since the context manager does all the "cleanup" work for you.

Upvotes: 9

Mayur Vora
Mayur Vora

Reputation: 962

Hello Maxine Mou,

Python 3 learning for best website,
1. https://www.tutorialspoint.com/python3/
2. https://docs.python.org/3/tutorial/
3. https://learnpythonthehardway.org/python3/

Difference between python 2 and python 3,
1. http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
2. https://www.quora.com/What-are-the-major-differences-between-Python-2-and-Python-3

The close() Method
The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.

Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Syntax

fileObject.close();

Your Mistake
When you write below function so it is not proper way because python is understood like as iterable object so you get error so only write lst.append(i).

lst = lst.append(i)

Second mistake same as first. If you are sorting the list using sort() function so don't need to store in any variable because Python is sorting in existing list. So only write lst.sort().

lst = lst.sort()

Try this below code,

    #Open File using open() file function
    fh = open("test.txt")
    lst = list() 
    words = fh.read()
    list1 = words.split()

    for item in list1: 
        if item in lst:
            continue
        else:
            lst.append(item)

    #List sorting using sort() function
    lst.sort()

    #Display the sorting list
    print(lst)

    # Close the file using close() method
    fh.close()

I hope my answer is helpful. If any query so please comment.

Upvotes: 0

Chris
Chris

Reputation: 22953

Your problem is this line:

lst = lst.append(i)

list.append() does not return a new list, it works in-place and returns None. That means you're only assigning lst to None. So when you try to use lst in your next iteration, it fails. Just remove the assignment to list.append():

lst.append(i)

Note you also make the same mistake here:

lst = lst.sort()

list.sort() works in-place. Remove the assignment.


Your code can also be improved in several ways:

  • Use the context manger to open your file. This ensures that your file will always close.

  • Since you appear to be filtering duplicates from your list, you can use a set instead to remove the duplicates, and sort the remaining words afterwards as @Julien suggested.

Here is how the new code would look:

with open('romeo.txt', 'r') as file:
    words = set(file.read().split())
    lst = sorted(words)

print(lst)

Upvotes: 4

almost a beginner
almost a beginner

Reputation: 1632

If you want to avoid the loop, you can use sets:

fh = open("romeo.txt")
words = fh.read()
lst = list(set(words.split()))
lst.sort()
print(lst)

Simply put, a set cannot have duplicates.

Upvotes: 0

Related Questions