How do I add values to an existing set
?

- 24,552
- 19
- 101
- 135

- 57,446
- 287
- 670
- 1,062
9 Answers
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.

- 24,552
- 19
- 101
- 135

- 854,459
- 170
- 1,222
- 1,395
-
142`.add` adds a single item, `.update` adds several items. Is that correct? – ThorSummoner Apr 06 '16 at 22:33
-
34@ThorSummoner Yes, `.add` expects a single hashable type, while `.update` expects an iterable object – Bonnie Nov 16 '16 at 00:15
-
8If you add a string with update, it will add one item per character in your string because it's an iterable! – hgolov Feb 07 '18 at 09:56
-
3It's indeed important to note that strings, as "whole" objects, should be added with `.add`, not `.update`, I recently faced quite a sneaky bug on account of this small detail. – Alexandre May 21 '18 at 17:52
-
2`add` and `update` change the set object, you can call `{*a, *b}` to return a new set object – Beatriz Fonseca Jan 10 '22 at 21:24
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.

- 14,854
- 11
- 100
- 103

- 5,406
- 2
- 20
- 30
-
1Really you edited my answer with the same answer? Thanks for contributing?? – RandallShanePhD May 26 '22 at 14:30
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 {...}
.

- 1,486
- 2
- 17
- 34

- 14,440
- 8
- 42
- 52
-
2If you have a list of sets `my_sets`, then you can do `big_set = my_sets[0].union(*my_sets[1:])` to combine them all – zelusp Nov 07 '16 at 01:24
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'}
.

- 386
- 7
- 15
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.

- 1,718
- 4
- 19
- 50
-
Converting to lists and back is a lot of unnecessary overhead and seems to defeat the purpose of sets. Consider the answer by @nyuszika7h as well as the solution in comments (ill copy here): `big_set = my_sets[0].union(*my_sets[1:])` – Razzle Shazl Dec 28 '20 at 01:13
I just wanted to add a quick note here. So I was looking for the fastest method among the three methods.
- Using the set.add() function
- Using the set.update() function
- 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:
set.add()
Took: 0.5208224999951199set.update()
Took: 0.6461397000239231 `- "|" operator` Took: 0.7649438999942504
PS: If you want to know more the analysis.
Check here : Fastest way to append values to set.

- 472
- 1
- 5
- 13
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...

- 2,380
- 3
- 18
- 31
keep.update((0,1,2,3,4,5,6,7,8,9,10))
Or
keep.update(np.arange(11))

- 712
- 6
- 18

- 1,524
- 1
- 19
- 26
-
2Your answer does not improve upon the existing answer from @sberry which was posted 9 years before your answer. – Razzle Shazl Dec 30 '20 at 23:53