How can I see the length of the item in the list instead of it being 1 all the time

by John-Peter Krause   Last Updated August 14, 2019 04:26 AM

I want to make a simple program that "removes" items from a list that aren't a specific length, but all the lengths are 1.

import csv

numberofcharacters = int(input("how many characters should the words have?"))

newlist = []

with open("C:\\Users\\stepk\\Desktop\\textexample.txt") as csvfile:
    csv_reader = csv.reader(csvfile, delimiter="\n")
    for i in csv_reader:
        print(len(i))
        if len(i) == numberofcharacters:
            newlist.append(i)
        else:
            print(i, " is getting removed")

print(newlist)

I expected:

how many characters should the words have? 3

5
['these']  is getting removed
4
['just']  is getting removed

... (expected something like this)

["idk","are"]

...

but i got:

how many characters should the words have?3
1
['idk']  is getting removed
1
['these']  is getting removed
1
['are']  is getting removed
1
['just']  is getting removed
1
['random']  is getting removed
1
['words']  is getting removed
1
['in']  is getting removed
1
['a']  is getting removed
1
['text']  is getting removed
1
['ducument']  is getting removed
[]


Related Questions


Updated October 15, 2018 23:26 PM

Updated March 30, 2017 22:26 PM

Updated May 09, 2018 09:26 AM

Updated August 11, 2018 10:26 AM