0

I have a list object and the list object in python is like below

[(<QA: QA object (8)>, 2), (<QA: QA object (12)>, 2), (<QA: QA object (7)>, 1), (<QA: QA object (9)>, 1), (<QA: QA object (13)>, 1), (<QA: QA object (14)>, 1), (<QA: QA object (15)>, 1), (<QA: QA object (16)>, 1), (<QA: QA object (17)>, 1)]

If you look at the aboev QA object, it is sorted based on a count where QA object (8) comes first as it is having a count of 2 and then the next QA object and so on....

But, when I want to capture only the QA object alone into a set, the sort is distorted.

for eachCandidate in alist:            
        wanted_items.add(eachCandidate[0])  
print(wanted_items)

The above print statement prints it as

{<QA: QA object (7)>, <QA: QA object (8)>, <QA: QA object (9)>, <QA: QA object (12)>, <QA: QA object (13)>, <QA: QA object (14)>, <QA: QA object (15)>, <QA: QA object (16)>, <QA: QA object (17)>}

Closely, when I look at this, <QA: QA object (7)> appears first but it should not appear and only QA object (8) should appear first.

How to make sure this happens. In other words, what can I do to avoid the distortion of the sort. Is there a way to knock of the count column in the list and convert the list to set.

I need to have the data in the set as Django framework returns only a set.

Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62

1 Answers1

0

sets do not maintain element order. You can have ordered sets by using different collections though. See this post for more info Does Python have an ordered set?

BeanBagTheCat
  • 435
  • 4
  • 7