Turning a list into a set - Python
15,105
If you drop the .split()
, you'll end up with your set of lines.
Comments
-
Karatawi almost 2 years
I wanted to print a set of lines in a file that start with a certain character (here it's "c"), but I get an error whenever I try to convert a list into a set
I have the following code:
z = open("test.txt", "r") wordList = [line.rstrip().split() for line in z if line.startswith(("c"))] wordList = set(wordList) print(wordList)
Here is the error I get:
Traceback (most recent call last): wordList = set(wordList) TypeError: unhashable type: 'list'
-
Karatawi about 8 yearsThank you dear sir, this solved my problem!
-
Karatawi about 8 yearsThanks man, this helped me too!