How to make string to be in set during enumerating

I have a question about the code that I have written.

My table_data is as follows:

table_data = [('|Office|Items|windows', 'furniture/metal/window/frame'),
     ('|Office|Items|chairs', 'furniture/metal/officeChairs/normal'),
     ('|Office|Items|chairs_2', 'furniture/metal/officeChairs/normal'),
     ('|Office|Items|doors|handles', 'furniture/metal/door/handle')]

And when I tried to do the following code, it works to my cause as it sort of ‘grouped’ the same name together such that it will shows only once…

for index, (full_path_name, extra_attr) in enumerate(table_data):
        row_position = self.table.rowCount()
        self.table.insertRow(row_position)            
        self.table.setItem(row_position, 0, QtGui.QTableWidgetItem(extra_attr))

Output result is as follows:

 Column1                                 | Column2
    furniture/metal/window/frame         |  
    furniture/metal/officeChairs/normal  |
    furniture/metal/door/handle          |

Noticed that furniture/metal/officeChairs/normal is only shown/populates once…

However, when I start to introduce in something new, so that when a particular radio button is checked…

 extra_attr_list = list()
    for index, (full_path_name, extra_attr) in enumerate(table_data):
        row_position = self.table.rowCount()
        self.table.insertRow(row_position)            
        self.table.setItem(row_position, 0, QtGui.QTableWidgetItem(extra_attr))
        extra_attr_list.append(extra_attr)
        
        if self.radio_button.isChecked():
            '''
            print extra_attr_list
            # Output Results :
            #       set(['furniture/metal/window/frame'])
            #       set(['furniture/metal/window/frame', 'furniture/metal/officeChairs/normal'])
            #       set(['furniture/metal/window/frame', 'furniture/metal/officeChairs/normal])
            #       set(['furniture/metal/window/frame', 'furniture/metal/door/handle', 'furniture/metal/officeChairs/normal'])
            '''
            for attr in set(extra_attr_list):
                self.table.setItem(row_position, 0, QtGui.QTableWidgetItem(attr))

I tried using list() and set() so as to filter out the ‘duplicated values’ but still furniture/metal/officeChairs/normal got displayed twice.

Output results:

Column1                                  | Column2
    furniture/metal/window/frame         |  
    furniture/metal/officeChairs/normal  |
    furniture/metal/officeChairs/normal  |
    furniture/metal/door/handle          |

What am I missing here? Can anyone advice?

in the example, you’re missing a quote in the second instance of furniture/metal/window/frame, so if you’re relying on that for uniqueness it might explain your problem.

It looks like you want a unique key value bound to a list of values… That sounds like more like dictionary, or an OrderedDict. That will ensure that the keys are unique. You can initialize the values of the dictionary to empy lists and then append to them as you populate so the data is all grouped the same way. The dictionary keys are effectively already a set so you won’t get duplicate entries.

[QUOTE=Theodox;30768]in the example, you’re missing a quote in the second instance of furniture/metal/window/frame, so if you’re relying on that for uniqueness it might explain your problem.

It looks like you want a unique key value bound to a list of values… That sounds like more like dictionary, or an OrderedDict. That will ensure that the keys are unique. You can initialize the values of the dictionary to empy lists and then append to them as you populate so the data is all grouped the same way. The dictionary keys are effectively already a set so you won’t get duplicate entries.[/QUOTE]

Hi Theodox, thanks for getting back to me.

I tried using dict and still I am getting the same issues in which when the radio button is checked, it is still populating all the 4 options instead of 3 in my table…

For full_path_name, it definitely works fine but not for extra_attr

If you had a dictionary already, wouldn’t extra_attr_list be the same as dictionary.values()? You’re appending to extra_attr_list for every key-value pair anway…

I’m really surprised that the first example only makes three rows. I don’t see how that could happen.

In the second example, when the radio button is checked, you are not incrementing the value of “row_position”. So you are just overwriting the last item multiple times.

Also, I am not sure why you are using “enumerate()” at all. I don’t see where you are using “index” anywhere.