Turning a list into a set - Python

15,105

If you drop the .split(), you'll end up with your set of lines.

Share:
15,105
Karatawi
Author by

Karatawi

"The enemy of your enemy, is your friend"

Updated on June 04, 2022

Comments

  • Karatawi
    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
    Karatawi about 8 years
    Thank you dear sir, this solved my problem!
  • Karatawi
    Karatawi about 8 years
    Thanks man, this helped me too!