Python Forum
How to append to a set in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append to a set in Python
#1
I am trying to append to a set and getting the following error:

AttributeError: 'dict' object has no attribute 'append'

lst = [3, 3, 22, 22, 1, 4, 5, 6, 10, 20, 40, 40, 40, 50]
dup_free = []
dup_free_set = {}
for x in lst:
    if x not in dup_free:
        dup_free.append(x)
        dup_free_set.append(x) # error here 
print(dup_free_set)
Is there a function/way to do this?

Best,
David
Reply
#2
First, line 3 doesn't create a set, but a dict as the error tells you. There isn't an empty set literal; if you want a set you have to use set().

In any case, appending doesn't make sense, because that means "adding to the end". Sets and dicts don't have the notion of order of items, so for a set you'd use the add method.
Reply
#3
Of course Python would run out of brackets.
Reply
#4
Thanks, I see.

Interested why the print output differs in lines 8,9.

lst = [3, 3, 22, 22, 1, 4, 5, 6, 10, 20, 40, 40, 40, 50]
dup_free = []
dup_free_set = {3, 3, 22, 22, 1, 4, 5, 6, 10, 20, 40, 40, 40, 50}
for x in lst:
    if x not in dup_free:
        dup_free.append(x)
        dup_free_set.add(x)
print(dup_free)
print(dup_free_set)

#[3, 22, 1, 4, 5, 6, 10, 20, 40, 50]
#{1, 3, 4, 5, 6, 40, 10, 50, 20, 22}
Reply
#5
The list on line 11 maintains the sequence in its original order. The dict on line 12 is essentially random. You may see what look like patterns but you can't count on them not to change for no apparent reason.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#6
They differ because they are different objects with different contents.

Besides the bracket differences, a list is a sequence. When you put an element at the end of a list (via append()), it stays in that position. A fixed order is one of the properties of the list.

The set doesn't maintain an order. The printed order depends on how many elements are in the set, and the hashed value of the object id()s within. If you need a particular order from a set, you have to establish it yourself. If you have to maintain a particular order, a set is the wrong collection to use.

print(sorted(dup_free_set))
Reply
#7
(Oct-01-2020, 03:25 PM)Marbelous Wrote: The dict on line 12 is essentially random.
That is not dict, but set. Otherwise you are right - it's unordered collection
Just to clear something about dicts - as of 3.7 dicts preserve insertion order. This was also true in 3.6 but as implementation detail.

Also let say that you can do
lst = [3, 3, 22, 22, 1, 4, 5, 6, 10, 20, 40, 40, 40, 50]
dup_free_set = set(lst)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
I corrected my post - I meant there isn't an empty set literal.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Including modules in Python using sys.path.append JoeDainton123 1 682 Aug-24-2020, 04:51 AM
Last Post: millpond
  How to append a tuple full of records to a dbf file in Python? DarkCoder2020 4 1,059 May-29-2020, 02:40 PM
Last Post: DarkCoder2020
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 693 Apr-22-2020, 01:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020