Python Forum
How to append to a set in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to append to a set in Python (/thread-30041.html)



How to append to a set in Python - dgrunwal - Oct-01-2020

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


RE: How to append to a set in Python - ndc85430 - Oct-01-2020

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.


RE: How to append to a set in Python - deanhystad - Oct-01-2020

Of course Python would run out of brackets.


RE: How to append to a set in Python - dgrunwal - Oct-01-2020

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}



RE: How to append to a set in Python - Marbelous - Oct-01-2020

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.


RE: How to append to a set in Python - bowlofred - Oct-01-2020

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))



RE: How to append to a set in Python - buran - Oct-01-2020

(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)



RE: How to append to a set in Python - ndc85430 - Oct-01-2020

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