Posts: 21
Threads: 8
Joined: Jun 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
Posts: 1,838
Threads: 2
Joined: Apr 2017
Oct-01-2020, 02:23 PM
(This post was last modified: Oct-01-2020, 04:26 PM by ndc85430.)
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.
Posts: 6,809
Threads: 20
Joined: Feb 2020
Of course Python would run out of brackets.
Posts: 21
Threads: 8
Joined: Jun 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}
Posts: 99
Threads: 1
Joined: Dec 2019
Oct-01-2020, 03:25 PM
(This post was last modified: Oct-01-2020, 03:26 PM by Marbelous.)
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
Posts: 1,583
Threads: 3
Joined: Mar 2020
Oct-01-2020, 03:29 PM
(This post was last modified: Oct-01-2020, 03:29 PM by bowlofred.)
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))
Posts: 8,166
Threads: 160
Joined: Sep 2016
Oct-01-2020, 03:32 PM
(This post was last modified: Oct-01-2020, 03:33 PM by buran.)
(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)
Posts: 1,838
Threads: 2
Joined: Apr 2017
I corrected my post - I meant there isn't an empty set literal.
|