[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to match list members in py3.x

On 11/25/18 12:28 PM, Muhammad Rizwan wrote:
> Below is my approach which I am sure is wrong somewhere and it doesn't work the way expected, the append doesn't do anything in my code nor does it returns any traceback all i see is a empty list printed at the end, I am using win10 x64 python 3.x atom-editor. Please take a look if you can tell me what am I doing wrong here. I want to check each list word by word if the word is not repeated in the same list it should be appended to a 'newlist'. Thanks in advance
> handle = open('file.txt')
> newlist = list()
> for line in handle:
>     #line.rstrip()
>     linewords = line.split()
>     print(linewords)
> for word in linewords:
>     newlist.append(word)
>     if word not in linewords:
>         continue
> print('new: ',newlist)

The first thing I note in looking at this is that the second loop will
run AFTER the first loop completes, not for each step of the loop,
because you outdented the code.

Also, you unconditionally add the word, and THEN check if it is in the list.

Richard Damon