Alex Gordon
Alex Gordon

Reputation: 60691

Append values to a set in Python

How do I add values to an existing set?

Upvotes: 585

Views: 800130

Answers (9)

AlixaProDev
AlixaProDev

Reputation: 560

I just wanted to add a quick note here. So I was looking for the fastest method among the three methods.

  1. Using the set.add() function
  2. Using the set.update() function
  3. Using the | operator function.

I find it out that to add either a single value or multiple values to a set you have to use the set.add() function. It is the most efficient method among the others.

So I ran a test and Here is the result:

  1. set.add() took: 0.520
  2. set.update() took: 0.646
  3. | operator took: 0.764

Upvotes: 3

Alex Martelli
Alex Martelli

Reputation: 881487

your_set.update(your_sequence_of_values)

e.g, your_set.update([1, 2, 3, 4]). Or, if you have to produce the values in a loop for some other reason,

for value in ...:
    your_set.add(value)

But, of course, doing it in bulk with a single .update call is faster and handier, when otherwise feasible.

Upvotes: 577

RandallShanePhD
RandallShanePhD

Reputation: 5774

Define a set

a = set()

Use add to append single values

a.add(1)
a.add(2)

Use update to add elements from tuples, sets, lists or frozen-sets

a.update([3, 4])
>>> print(a)
{1, 2, 3, 4}

Note: Since set elements must be hashable, and lists are considered mutable, you cannot add a list to a set. You also cannot add other sets to a set. You can however, add the elements from lists and sets as demonstrated with the .update method.

Upvotes: 455

Kira Resari
Kira Resari

Reputation: 2420

The way I like to do this is to convert both the original set and the values I'd like to add into lists, add them, and then convert them back into a set, like this:

setMenu = {"Eggs", "Bacon"}
print(setMenu)
> {'Bacon', 'Eggs'}
setMenu = set(list(setMenu) + list({"Spam"}))
print(setMenu)
> {'Bacon', 'Spam', 'Eggs'}
setAdditions = {"Lobster", "Sausage"}
setMenu = set(list(setMenu) + list(setAdditions))
print(setMenu)
> {'Lobster', 'Spam', 'Eggs', 'Sausage', 'Bacon'}

This way I can also easily add multiple sets using the same logic, which gets me an TypeError: unhashable type: 'set' if I try doing it with the .update() method.

Upvotes: 1

rishi jain
rishi jain

Reputation: 1640

keep.update((0,1,2,3,4,5,6,7,8,9,10))

Or

keep.update(np.arange(11))

Upvotes: -1

decadenza
decadenza

Reputation: 2578

For me, in Python 3, it's working simply in this way:

keep = keep.union((0,1,2,3,4,5,6,7,8,9,10))

I don't know if it may be correct...

Upvotes: -1

user492203
user492203

Reputation:

You can also use the | operator to concatenate two sets (union in set theory):

>>> my_set = {1}
>>> my_set = my_set | {2}
>>> my_set
{1, 2}

Or a shorter form using |=:

>>> my_set = {1}
>>> my_set |= {2}
>>> my_set
{1, 2}

Note: In versions prior to Python 2.7, use set([...]) instead of {...}.

Upvotes: 156

Alexandre
Alexandre

Reputation: 415

This question is the first one that shows up on Google when one looks up "Python how to add elements to set", so it's worth noting explicitly that, if you want to add a whole string to a set, it should be added with .add(), not .update().

Say you have a string foo_str whose contents are 'this is a sentence', and you have some set bar_set equal to set().

If you do bar_set.update(foo_str), the contents of your set will be {'t', 'a', ' ', 'e', 's', 'n', 'h', 'c', 'i'}.

If you do bar_set.add(foo_str), the contents of your set will be {'this is a sentence'}.

Upvotes: 20

sberry
sberry

Reputation: 131968

Use update like this:

keep.update(newvalues)

Upvotes: 47

Related Questions