64

How do I get the contents of a set() in list[] form in Python?

I need to do this because I need to save the collection in Google App Engine and Entity property types can be lists, but not sets. I know I can just iterate over the whole thing, but it seems like there should be a short-cut, or "best practice" way to do this.

martineau
  • 119,623
  • 25
  • 170
  • 301
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
  • @Franklin I am tempted to close as general reference http://chat.meta.stackoverflow.com/transcript/message/654451#654451 – Trufa Jun 19 '11 at 22:57
  • 4
    @Franklin: I searched in general search and then I posted variants of the subject line in Stackoverflow to make sure it wasn't a dupe. This is the kind of information that should be represented here on Stackoverflow.com – Chris Dutrow Jun 19 '11 at 23:01
  • 1
    @Franklin: I don't think this one is really easy to find with Google. Searches like "python set list" or similar will return more general documentation – Sven Marnach Jun 19 '11 at 23:04
  • @Sven Marnach In the first 10 google results there are 2 links that would help you if you try it in the interactive console: http://ircarchive.info/python/2007/5/8/76.html and http://www.daniweb.com/software-development/python/threads/93829 but I think this is an edge case, since the documentation is not really authoritative and not "very" easily accessible. – Trufa Jun 19 '11 at 23:07
  • @Sven Marnach: Yeah, especially if you're just learning python. The first place you'll look is the methods associated with "set" such as "add()" and "update()". You will be frustrated when there is nothing available like "get_list" or "get_iterable" and then you won't know where to look next. – Chris Dutrow Jun 19 '11 at 23:09
  • @Trufa: Google is poor place to search for answers to programming questions. This is why I use Stackoverflow. I wasn't aware that Stackoverflow was only supposed to represent information that could not be found on google – Chris Dutrow Jun 19 '11 at 23:12
  • @DutrowLLC I didn't downvote your question and nor vote to close it. BUT google is a great place for programmers!! I use stackoverflow a lot and it is in fact very helpful **it is not a replacement for basic search**. I'm not just inventing that, I heard the creators of the site repeat that thousands of times, it's just not, I think your question is an edge case because there is no clear evidence on the first google results. Even so, in python it is very common to use things like `int(3.5)`,`float(2)`,`str(4)`,`int("45")`, `list("hello")` and so on and it might be most python dev first try BUT – Trufa Jun 19 '11 at 23:16
  • you dont have to know that, we are all (myself definitely included) here to learn. I just want to stress the point that you should not by any means replace the normal basic search, rather use it to answer you specific programming questions. – Trufa Jun 19 '11 at 23:20
  • @Trufa: I'm not active on chat or meta so I'm not intimately familiar with the creators and their vision for the site. But I suspect there is a disconnect between how upper echelon programmers such as the creators and people with very high feedback are using the site and people like me who are merely competent programmers. Example: this particular question normally would have been answered already, so someone just learning python could use Stackoverflow as a pivotal tool for learning the language. I suspect much traffic comes from this sort of use case. But an expert would not know this. – Chris Dutrow Jun 19 '11 at 23:30
  • @DutrowLLC: Again this is an edge case but the thing is, that it is important for the site owners to keep the experts answerers interested, they are in a way what make this site what it is. See http://blog.stackoverflow.com/2011/06/optimizing-for-pearls-not-sand/ – Trufa Jun 19 '11 at 23:34
  • @DutrowLLC: also take a look at this: http://meta.stackexchange.com/questions/86043/introduce-a-general-reference-close-reason/88521#88521 – Trufa Jun 19 '11 at 23:39
  • @Trufa: If you had deleted this question, then when someone else came looking to this site for this answer and don't find it. One of two things would have happened: 1) They would be forced to look elsewere, reducing the quality of the site from their perspective, or 2) They would re-post the question and thus utilize twice as much resources from question askers, answerers, and moderators. – Chris Dutrow Jun 19 '11 at 23:46
  • @Trufa: I'm not arguing your interpretation of what is being said on chat and meta. I am arguing that what is being said there represents a flawed strategy. – Chris Dutrow Jun 19 '11 at 23:48
  • @DutrowLLC: I don't think it is a wrong strategy, the site has, in two years, become one of the top 200 sites on the internet. That, you might agree, is quite an achievement! The main reason this could be achieved is by keeping the quality of the questions and answers, even if it meant pushing some people away, you have to keep the awesome people, experts in the field that answer questions happy above all. – Trufa Jun 19 '11 at 23:54
  • @Trufa: My primary area of expertise is business, not programming. And one mistake I often see in business is failure to examine your business from a perspective opposite of your own. If you sell widgets, and are an expert on widgets. The one thing you are not an expert on, is what its like to be someone who is not an expert on widgets, which unfortunately can be exactly the kind of person who your customer might be. In this case, the one thing that an expert programmer is NOT an expert on, is what its like not to be an expert programmer. – Chris Dutrow Jun 19 '11 at 23:54
  • @DutrowLLC: this has gotten to chatty: http://chat.meta.stackoverflow.com/rooms/395/chat-for-trufa-dutrowllc – Trufa Jun 19 '11 at 23:56
  • @Trufa: I've been around for over 2 years and am in the top 22% of users on this site, am I the kind of person you propose pushing away? Did the presence of this question make you unahppy, or did it make you happy that you found an issue that you thought you might be able to moderate? Did you notice that 4 people upvoted this question? Normally this type of question would not get so many up-votes, so I assume they are there from people who saw the downvotes and wanted to counter-act them since they also often use the site for the same purpose as I do. – Chris Dutrow Jun 20 '11 at 00:00
  • just type `help(list)` on your python prompt: `list(iterable) -> new list initialized from iterable's items`. The question was too obvious. – JBernardo Jun 20 '11 at 00:05
  • @JBernardo: Someone who did not know the answer to this question would likely also not know to do what you just said. – Chris Dutrow Jun 20 '11 at 00:09

3 Answers3

78
>>> s = set([1, 2, 3])
>>> list(s)
[1, 2, 3]

Note that the list you get doesn't have a defined order.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 24
    Explanation of this: `list` accepts an iterable argument which will be used to populate the list. So as `set` is iterable, it produces the desired effect. – Chris Morgan Jun 19 '11 at 22:41
78

See Sven's answer, but I would use the sorted() function instead: that way you get the elements in a nice predictable order (so you can compare the lists afterwards, for example).

>>> s = set([1, 2, 3, 4, 5])
>>> sorted(s)
[1, 2, 3, 4, 5]

Of course, the set elements have to be sortable for this to work. You can't sort complex numbers (see gnibbler's comment). In Python 3, you also can't sort any set with mixed data types, e.g. set([1, 2, 'string']).

You can use sorted(s, key=str), but it may not be worth the trouble in these cases.

Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81
  • 11
    It's not a good idea to sort stuff "just in case". Sorting is less efficient than calling list, but more importantly - what if the elements of the set are not sortable? eg `s=set((1+1j,2+2j,3+3j))` – John La Rooy Jun 20 '11 at 00:26
  • Thanks for pointing it out! I've updated the answer with a bit more info on this. – Petr Viktorin Jun 21 '11 at 20:07
  • (As far as I know, Google App Engine entities only support sortable types. And I personally don't usually care about a bit of extra processing if I can get data stored in a normal form... Anyway, it's just an alternative that might be useful in some cases. ☺) – Petr Viktorin Jun 21 '11 at 20:14
  • Thank you. The sorted result help me to solve one problem on [codeEval](http://codeeval.com/). Without `sorted`, it will fail at one test case. – AechoLiu Oct 09 '14 at 07:43
2
>>> a = [1, 2, 3, 4, 2, 3]
>>> b = set(a)
>>> b
set([1, 2, 3, 4])
>>> c = list(b)
>>> c
[1, 2, 3, 4]
>>> 
sam
  • 18,509
  • 24
  • 83
  • 116