Randomblue
Randomblue

Reputation: 116283

Add number to set

What am I doing wrong here?

a = set().add(1)
print a # Prints `None`

I'm trying to add the number 1 to the empty set.

Upvotes: 45

Views: 97360

Answers (8)

Antoni
Antoni

Reputation: 342

Alternatively to a = set() | {1} consider "in-place" operator:

a = set()
a |= {1}

Upvotes: 2

user4289108
user4289108

Reputation: 51

Another way to do it that is relatively simple would be:

a = set()
a = set() | {1}

this creates a union between your set a and a set with 1 as the element

print(a) yields {1} then because a would now have all elements of both a and {1}

Upvotes: 5

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

It is a convention in Python that methods that mutate sequences return None.

Consider:

>>> a_list = [3, 2, 1]
>>> print a_list.sort()
None
>>> a_list
[1, 2, 3]

>>> a_dict = {}
>>> print a_dict.__setitem__('a', 1)
None
>>> a_dict
{'a': 1}

>>> a_set = set()
>>> print a_set.add(1)
None
>>> a_set
set([1])

Some may consider this convention "a horrible misdesign in Python", but the Design and History FAQ gives the reasoning behind this design decision (with respect to lists):

Why doesn’t list.sort() return the sorted list?

In situations where performance matters, making a copy of the list just to sort it would be wasteful. Therefore, list.sort() sorts the list in place. In order to remind you of that fact, it does not return the sorted list. This way, you won’t be fooled into accidentally overwriting a list when you need a sorted copy but also need to keep the unsorted version around.

In Python 2.4 a new built-in function – sorted() – has been added. This function creates a new list from a provided iterable, sorts it and returns it.

Your particular problems with this feature come from a misunderstanding of good ways to create a set rather than a language misdesign. As Lattyware points out, in Python versions 2.7 and later you can use a set literal a = {1} or do a = set([1]) as per Sven Marnach's answer.

Parenthetically, I like Ruby's convention of placing an exclamation point after methods that mutate objects, but I find Python's approach acceptable.

Upvotes: 64

Sven Marnach
Sven Marnach

Reputation: 601609

The add() method adds an element to the set, but it does not return the set again -- it returns None.

a = set()
a.add(1)

or better

a = set([1])

would work.

Upvotes: 28

Gareth Latty
Gareth Latty

Reputation: 89007

You are assigning the value returned by set().add(1) to a. This value is None, as add() does not return any value, it instead acts in-place on the list.

What you wanted to do was this:

a = set()
a.add(1)
print(a)

Of course, this example is trivial, but Python does support set literals, so if you really wanted to do this, it's better to do:

a = {1}
print(a)

The curly brackets denote a set (although be warned, {} denotes an empty dict, not an empty set due to the fact that curly brackets are used for both dicts and sets (dicts are separated by the use of the colon to separate keys and values.)

Upvotes: 2

PaulMcG
PaulMcG

Reputation: 63709

The add method updates the set, but returns None.

a = set()
a.add(1)
print a

Upvotes: 2

Óscar López
Óscar López

Reputation: 236004

You should do this:

a = set()
a.add(1)
print a

Notice that you're assigning to a the result of adding 1, and the add operation, as defined in Python, returns None - and that's what is getting assigned to a in your code.

Alternatively, you can do this for initializing a set:

a = set([1, 2, 3])

Upvotes: 2

Rik Poggi
Rik Poggi

Reputation: 29302

Because add() is modifing your set in place returning None:

>>> empty = set()
>>> print(empty.add(1))
None
>>> empty
set([1])

Upvotes: 5

Related Questions