If you have a list of hashable objects (filenames would probably be strings, so they should count):
lst = ['foo.py', 'bar.py', 'baz.py', 'qux.py', Ellipsis]
you can construct the set directly:
s = set(lst)
In fact, set will work this way with any iterable object! (Isn't duck typing great?)
If you want to do it iteratively:
s = set()
for item in iterable:
s.add(item)
But there's rarely a need to do it this way. I only mention it because the set.add method is quite useful.
If you have a list of hashable objects (filenames would probably be strings, so they should count):
lst = ['foo.py', 'bar.py', 'baz.py', 'qux.py', Ellipsis]
you can construct the set directly:
s = set(lst)
In fact, set will work this way with any iterable object! (Isn't duck typing great?)
If you want to do it iteratively:
s = set()
for item in iterable:
s.add(item)
But there's rarely a need to do it this way. I only mention it because the set.add method is quite useful.
The most direct solution is this:
s = set(filelist)
The issue in your original code is that the values weren't being assigned to the set. Here's the fixed-up version of your code:
s = set()
for filename in filelist:
s.add(filename)
print(s)
Greetings all,
I'm learning the very basics of Python and I'm trying to convert a list to a set in order to remove duplicate words in the list. That's possible, right?
What I have so far is as follows:
def remove_dups(self):
""" Return a new list in which all duplicates from the words, have been removed. """
words = ["show", "show"]
uniquewords = set(words)
print(remove_dups)
This is part of a larger code, but this last part is my problem area. The list of words is longer, but I deleted them to save space in this thread. Is my formatting wrong? I'm not able to get anything to print.
I'd greatly appreciate the help!
How to convert a set to a list in python? - Stack Overflow
Converting a set to list
Creating a list from a set
convert set to a list type in python
It is already a list:
>>> type(my_set)
<class 'list'>
Do you want something like:
>>> my_set = set([1, 2, 3, 4])
>>> my_list = list(my_set)
>>> print(my_list)
[1, 2, 3, 4]
EDIT: Output of your last comment:
>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print(my_new_list)
[1, 2, 3, 4]
I'm wondering if you did something like this:
>>> set = set()
>>> set([1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable
Instead of:
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)
Why not shortcut the process:
my_list = list(set([1,2,3,4])
This will remove the dupes from you list and return a list back to you.
One problem I am trying to solve is to extract the unique items from an array into another array. I thought that I would convert the array into a set and cast it back into an array.
It works if i let a for loop run and append the items into a new list as follows:
lizst = [1,1,1,2,3,3,3,3,4,4]
def uniq(lst):
to_set = set(lst)
to_list = []
for item in to_set:
to_list.append(item)
return to_listhowever, then i tried to be more intelligent and tried this:
def uniq2(lst):
return list(set(lst))however, when I call this function as follows:
uniq2(lizst)
i get:
<ipython-input-23-2cb8fd02733f> in uniq2(lst) 1 def uniq2(lst): ----> 2 return list(set(lst)) TypeError: 'list' object is not callable
why am i getting this error? is there no list() function? how am i supposed to cast the set to a list then?