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

12019 questions
1221
votes
33 answers

Get difference between two lists with Unique Entries

I have two lists in Python: temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One', 'Two'] Assuming the elements in each list are unique, I want to create a third list with items from the first list which are not in the second list: temp3 =…
Max Frai
  • 61,946
  • 78
  • 197
  • 306
821
votes
19 answers

How to convert an Array to a Set in Java

I would like to convert an array to a Set in Java. There are some obvious ways of doing this (i.e. with a loop) but I would like something a bit neater, something like: java.util.Arrays.asList(Object[] a); Any ideas?
user130076
728
votes
16 answers

Does Python have an ordered set?

Python has an ordered dictionary. What about an ordered set?
Casebash
  • 114,675
  • 90
  • 247
  • 350
654
votes
15 answers

How to retrieve an element from a set without removing it?

Suppose the following: >>> s = set([1, 2, 3]) How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to…
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
598
votes
15 answers

Convert Set to List without creating new List

I am using this code to convert a Set to a List: Map> mainMap = new HashMap<>(); for (int i=0; i < something.size(); i++) { Set set = getSet(...); //returns different result each time List listOfNames = new…
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
594
votes
7 answers

Empty set literal?

[] = empty list () = empty tuple {} = empty dict Is there a similar notation for an empty set? Or do I have to write set()?
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
560
votes
7 answers

C# Set collection?

Does anyone know if there is a good equivalent to Java's Set collection in C#? I know that you can somewhat mimic a set using a Dictionary or a HashTable by populating but ignoring the values, but that's not a very elegant way.
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
553
votes
9 answers

Append values to a set in Python

How do I add values to an existing set?
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
509
votes
26 answers

What is the difference between Set and List?

What is the fundamental difference between the Set and List interfaces?
Johanna
  • 27,036
  • 42
  • 89
  • 117
461
votes
12 answers

How to check that an element is in a std::set?

How do you check that an element is in a set? Is there a simpler equivalent of the following code: myset.find(x) != myset.end()
fulmicoton
  • 15,502
  • 9
  • 54
  • 74
432
votes
22 answers

Getting an element from a Set

Why doesn't Set provide an operation to get an element that equals another element? Set set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains…
foobar
  • 4,968
  • 3
  • 17
  • 11
421
votes
9 answers

How to get the first element of the List or Set?

I'd like to know if I can get the first element of a list or set. Which method to use?
user496949
  • 83,087
  • 147
  • 309
  • 426
396
votes
13 answers

Add list to set

How do I add a list of values to an existing set?
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
383
votes
7 answers

Best way to find the intersection of multiple sets?

I have a list of sets: setlist = [s1,s2,s3...] I want s1 ∩ s2 ∩ s3 ... I can write a function to do it by performing a series of pairwise s1.intersection(s2), etc. Is there a recommended, better, or built-in way?
user116293
  • 5,534
  • 4
  • 25
  • 17
337
votes
13 answers

In Python, when to use a Dictionary, List or Set?

When should I use a dictionary, list or set? Are there scenarios that are more suited for each data type?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
1
2 3
99 100