How to construct a set out of list items in python?

How to construct a set out of list items in python?



I have a list of filenames in python and I would want to construct a set out of all the filenames.


list


set


filelist=
for filename in filelist:
set(filename)



This does not seem to work. How can do this?




4 Answers
4



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?)


set



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.


set.add





I am using 2.7 and this does not seem to work. What's going on here? Python 2.7.13 (default, Jul 24 2017, 14:22:59) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = [1,2,3] >>> set(x) set([1, 2, 3]) >>>
– user1902291
Dec 20 '17 at 18:49






@user1902291-- It looks like it worked, you just neek to keep track of the resulting set: y = set(x)
– mgilson
Dec 21 '17 at 20:38


y = set(x)





Also note that adding items in the set from a list can prove to be very useful when you want to filter out duplicates from the list.
– Pranjal Kumar
Jul 3 at 5:29



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)



You can do


my_set = set(my_list)



or, for Python 3,


my_set = *my_list



to create a set from a list. Conversely, you can also do


my_list = list(my_set)



or, for Python 3,


my_list = [*my_set]



to create a list from a set.



Just note that the order of the elements in a list is generally lost when converting the list to a set since a set is inherently unordered. (One exception in CPython, though, seems to be if the list consists only of non-negative integers, but I assume this is a consequence of the implementation of sets in CPython and that this behavior can vary between different Python implementations.)



Here is another solution:


>>>list1=["C:\","D:\","E:\","C:\"]
>>>set1=set(list1)
>>>set1
set(['E:\', 'D:\', 'C:\'])



In this code I have used the set method in order to turn it into a set and then it removed all duplicate values from the list



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế