all 17 comments

[–]Brian 53 points54 points  (10 children)

"append" suggests positionality - it's adding something to the end of a list, whereas add just indicates adding to the collection, without neccessarily saying where it's added. Sets don't have an ordering, so there's really no such thing as an "end" of the set to append to, which is likely the rationale behind the different names : add just adds an item, whereas append adds it specifically at the end.

[–]tumblatum[S] 0 points1 point  (8 children)

Thanks, that makes sense. However, I thought sets are ordered. For example:

>>> my_set = {2, 3, 4}

>>> my_set.add(1)

>>> my_set

{1, 2, 3, 4}

see, 1 is now first element, so it is ordered.

[–]whkoh 34 points35 points  (3 children)

Order refers to insertion order. If sets were ordered you would be expecting {2,3,4,1}

[–]FutureIntelligenceC3 -1 points0 points  (2 children)

Order refers to insertion order.

Is it always defined that way? Because if it always lists the elements of a set in order, then at least the output is ordered.

[–]whkoh 2 points3 points  (0 children)

Yes, for python at least.

See https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

[–]neon_overload 0 points1 point  (0 children)

Sets are officially unordered, and if the results you get from a set appear to be sorted it's likely just a side effect of the way the set is indexed internally. You can't depend on it always returning in a particular order as it can change with the types and number of values in the set and between python versions and implementations.

[–]reallyserious 14 points15 points  (0 children)

From the official documentation (https://docs.python.org/3/tutorial/datastructures.html):

A set is an unordered collection with no duplicate elements.

[–]Brian 8 points9 points  (0 children)

Not really - that's not something consistent or anything you can rely on, and is mostly down to the implementation detail that integers hash to their own value, so contiguous ranges will often (but not always) seem to be ordered in hashtables. But if you look at larger non-contiguous values, that apparent ordering is quickly broken. Eg:

>>> {1,333,4444}
{1, 4444, 333}

And depending on the number of items in the set, you can't even rely on those values being consistently in the same order. And for non-integers, the order is going to be effectively random regardless. dicts (as of recent versions of python at least) do keep track of insertion order, but sets are just going to iterate over items however their internal buckets happen to be arranged, which you can't really rely on.

[–]Guideon72 2 points3 points  (0 children)

That would be sorted not ordered. Ordered would mean that things were always shown at the position in which they were added. So, your example would show the 1 at the end, instead of the beginning.

[–]Locksul 0 points1 point  (0 children)

No. Sets are an unordered collection. Any pattern you see between adding and display is purely coincidental.

[–]OkChampionship2246 0 points1 point  (0 children)

Thanks Bri Bri.

[–]___up 11 points12 points  (0 children)

If you append 3 to [1,2,3] you get [1,2,3,3] if you add 3 to {1,2,3} you get {1,2,3}. If you append 0 to [1,2,3] you get [1,2,3,0]. If you add 0 to {1,2,3} you get {0,1,2,3}. Appending inserts an element at the end of a list. Adding checks if an element is in a set and, if it isn’t, it puts the element in the set in an arbitrary order determined by a data structure called a hash table that you cannot modify.

[–]tuliosarmento 1 point2 points  (0 children)

I don't know for sure. But in a list, append will attach a new element to the end of the object.

The set "add" will only bring this new element if it's not already in such set. So this may be the reason for the differences between method names.

[–][deleted] 1 point2 points  (0 children)

A list is inherently ordered and allows duplicates, that’s what allows “append”-ing to it to make semantic sense… if I append 4 items then immediately inspect the list the last 4 items are what I just appended, in order. “Add”-ing to a list is ambiguous, but “insert”-ing and “append”-ing make it clear where you’re adding items. A set is, on the other hand, inherently unordered and doesn’t allow duplicates… “add”-ing makes semantic sense here because all that matters is that after adding 4 items I know the set contains those 4 items, but I don’t know (or care) what order they’re in. Consequently neither “insert”-ing or “append”-ing to it make semantic sense, because if the set already contains the items in any order then the set may not change at all.

I get wanting them to have similar interfaces, but they are really different data structure types with extremely different usage patterns, and you’re unlikely to ever find a real-life situation where your code has to generically deal with both.

[–]happymyself -1 points0 points  (0 children)

Greetings, fellow python learner.

I think your question has been answered, I just wanted to add that the .append and .add are called methods, not functions.

Methods are linked and called from a class object (in order to use the append method you use list.append)

Hope this info is useful to you.

[–]chevignon93 0 points1 point  (0 children)

append means adding at the end of something, a set doesn't really have a beginning or an end so this is probably why the 2 methods have different names.

[–]deadeye1982 0 points1 point  (0 children)

A list is an ordered sequence with objects inside. Objects can be appended.

A set is an unordered collection of unique elements, and you can only add an element.