Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am having a list of filenames in python and from that list, I want to create a set out of all the filenames.

I have tried the below code, but I am getting some error, kindly check:

filelist=[]

for filename in filelist:

    set(filename)

1 Answer

0 votes
by (107k points)

See if you are having a list of hashable objects like the filenames would apparently be strings, so they should count as a washable object:

lst = ['foo.py', 'bar.py', 'baz.py', 'qux.py', Ellipsis]

You can create the set directly with the set() in Python:

s = set(lst)

In fact, the set will work this way with any iterable object.

Related questions

+1 vote
1 answer
asked Jul 31, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...