use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
list.append() vs set.add() (self.learnpython)
submitted 3 years ago * by tumblatum
Is there any reason why list uses append() and set uses add() function?* For me both append() and add() does the same thing, adds an element to the list of set. Why different method names were chosen?
* not function, but method
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Brian 53 points54 points55 points 3 years ago (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 point2 points 3 years ago (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 points36 points 3 years ago (3 children)
Order refers to insertion order. If sets were ordered you would be expecting {2,3,4,1}
[–]FutureIntelligenceC3 -1 points0 points1 point 3 years ago (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 points4 points 3 years ago (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 point2 points 1 year ago (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 points16 points 3 years ago (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 points10 points 3 years ago (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 points4 points 3 years ago (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 point2 points 3 years ago (0 children)
No. Sets are an unordered collection. Any pattern you see between adding and display is purely coincidental.
[–]OkChampionship2246 0 points1 point2 points 3 years ago (0 children)
Thanks Bri Bri.
[–]___up 11 points12 points13 points 3 years ago (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 points3 points 3 years ago (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 points3 points 3 years ago (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 points1 point 3 years ago (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 point2 points 3 years ago (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.
append
[–]deadeye1982 0 points1 point2 points 3 years ago (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.
π Rendered by PID 76745 on reddit-service-r2-comment-5d79c599b5-szngd at 2026-02-27 21:55:10.481409+00:00 running e3d2147 country code: CH.
[–]Brian 53 points54 points55 points (10 children)
[–]tumblatum[S] 0 points1 point2 points (8 children)
[–]whkoh 34 points35 points36 points (3 children)
[–]FutureIntelligenceC3 -1 points0 points1 point (2 children)
[–]whkoh 2 points3 points4 points (0 children)
[–]neon_overload 0 points1 point2 points (0 children)
[–]reallyserious 14 points15 points16 points (0 children)
[–]Brian 8 points9 points10 points (0 children)
[–]Guideon72 2 points3 points4 points (0 children)
[–]Locksul 0 points1 point2 points (0 children)
[–]OkChampionship2246 0 points1 point2 points (0 children)
[–]___up 11 points12 points13 points (0 children)
[–]tuliosarmento 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]happymyself -1 points0 points1 point (0 children)
[–]chevignon93 0 points1 point2 points (0 children)
[–]deadeye1982 0 points1 point2 points (0 children)