Questions tagged [set]

A set is a collection in which no element is repeated, which may be able to enumerate its elements according to an ordering criterion (an "ordered set") or retain no order (an "unordered set").

A set is a collection in which no element is repeated. It is often implemented by hashing the objects as they are added to the set, and comparing against those hashes for operations on the set.

In the C++ standard library in particular, the std::set is able to enumerate its elements according to a specific strict weak ordering criterion set on container construction. To achieve this, it is typically implemented by a binary tree. By contrast, the std::unordered_set stores unique elements in no particular order, and allows for fast retrieval of individual elements based on their value.

In Python, there are currently two built-in set types, set and frozenset. set is mutable, i.e. the contents can be changed and it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable.

Common operations on sets:

  • add
  • remove
  • find (check membership)
  • union, intersection, difference

Resources

10695 questions
296
votes
12 answers

Add list to set?

Tested on Python 2.6 interpreter: >>> a=set('abcde') >>> a set(['a', 'c', 'b', 'e', 'd']) >>> l=['f','g'] >>> l ['f', 'g'] >>> a.add(l) Traceback (most recent call last): File "", line 1, in a.add(l) TypeError: list…
Adam Matan
  • 107,447
  • 124
  • 346
  • 512
293
votes
8 answers

How to Iterate over a Set/HashSet without an Iterator?

How can I iterate over a Set/HashSet without the following? Iterator iter = set.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); }
user1621988
  • 3,825
  • 8
  • 24
  • 31
243
votes
18 answers

Why doesn't java.util.Set have get(int index)?

I'm sure there's a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method? It seems that sets are great for putting things into, but I can't find an elegant way of retrieving a…
Marty Pitt
  • 26,266
  • 34
  • 115
  • 190
235
votes
5 answers

How to "perfectly" override a dict?

How can I make as "perfect" a subclass of dict as possible? The end goal is to have a simple dict in which the keys are lowercase. It would seem that there should be some tiny set of primitives I can override to make this work, but according to all…
Paul Biggar
  • 24,430
  • 19
  • 91
  • 139
228
votes
2 answers

How to calculate the intersection of two sets?

Possible Duplicate: Efficiently finding the intersection of a variable number of sets of strings Say, have two Hashset, how to calculate the intersection of them? Set s1 = new HashSet(); Set s2 = new…
user496949
  • 75,601
  • 138
  • 297
  • 413
214
votes
13 answers

Simplest way to merge ES6 Maps/Sets?

Is there a simple way to merge ES6 Maps together (like Object.assign)? And while we're at it, what about ES6 Sets (like Array.concat)?
jameslk
  • 3,258
  • 3
  • 16
  • 17
214
votes
9 answers

Python Sets vs Lists

In Python, which data structure is more efficient/speedy? Assuming that order is not important to me and I would be checking for duplicates anyway, is a Python set slower than a Python list?
Mantas Vidutis
  • 15,230
  • 20
  • 73
  • 91
212
votes
6 answers

Swift Set to Array

An NSSet can be converted to Array using set.allObjects() but there is no such method in the new Set (introduced with Swift 1.2). It can still be done by converting Swift Set to NSSet and use the allObjects() method but that is not optimal.
Fried Rice
  • 2,929
  • 3
  • 16
  • 21
208
votes
7 answers

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. filelist=[] for filename in filelist: set(filename) This does not seem to work. How can do this?
securecoding
  • 2,143
  • 2
  • 13
  • 14
204
votes
6 answers

Most concise way to convert a Set to a List

For example, I am currently doing this: Set setOfTopicAuthors = .... List list = Arrays.asList( setOfTopicAuthors.toArray( new String[0] ) ); Can you beat this ?
Jacques René Mesrine
  • 41,077
  • 24
  • 60
  • 99
197
votes
13 answers

Java Set retain order?

Does a Java Set retain order? A method is returning a Set to me and supposedly the data is ordered but iterating over the Set, the data is unordered. Is there a better way to manage this? Does the method need to be changed to return something…
user840930
  • 4,124
  • 19
  • 54
  • 85
196
votes
8 answers

How to customize object equality for JavaScript Set

New ES 6 (Harmony) introduces new Set object. Identity algorithm used by Set is similar to === operator and so not much suitable for comparing objects: var set = new Set(); set.add({a:1}); set.add({a:1}); console.log([...set.values()]); // Array […
czerny
  • 11,433
  • 12
  • 57
  • 80
195
votes
16 answers

Set value of hidden field in a form using jQuery's ".val()" doesn't work

I've been trying to set the value of a hidden field in a form using jQuery, but without success. Here is a sample code that explains the problem. If I keep the input type to "text", it works without any trouble. But, changing the input type to…
texens
  • 3,277
  • 3
  • 19
  • 21
192
votes
4 answers

JavaScript Array to Set

MSDN references JavaScript's Set collection abstraction. I've got an array of objects that I'd like to convert to a set so that I am able to remove (.delete()) various elements by name: var array = [ {name: "malcom", dogType: "four-legged"}, …
Thomas
  • 4,827
  • 5
  • 28
  • 60
192
votes
8 answers

How to join two sets in one line without using "|"

Assume that S and T are assigned sets. Without using the join operator |, how can I find the union of the two sets? This, for example, finds the intersection: S = {1, 2, 3, 4} T = {3, 4, 5, 6} S_intersect_T = { i for i in S if i in T } So how can…
fandyst
  • 2,470
  • 2
  • 11
  • 15