0

I have a UI I wrote that in which I place all my widgets in a list through a list comprehension. I had originally attempted to place everything in a dictionary comprehension so that everything would have a label.

widgets = [
    QtGui.QLineEdit(),
    QtGui.QSpinBox(),
    QtGui.QLabel("-"),
    QtGui.QSpinBox(),
    QtGui.QPushButton("Export"),
    QtGui.QPushButton("Del")
]

as opposed to

widgets = {
    "textField":QtGui.QLineEdit(),
    "minFrame":QtGui.QSpinBox(),
    "dash":QtGui.QLabel("-"),
    "maxFrame":QtGui.QSpinBox(),
    "exportButton":QtGui.QPushButton("Export"),
    "delButton":QtGui.QPushButton("Del")
}

My question here is why is it that when I implement the dictionary, the order of the elements seems to mix up. So when I loop through to add them to the layout like so:

for w in widgets: # or widgets.values() for the dic
    mainLayout.addWidget(w)

It seems like the dictionary is slower to initialize all the objects than a list.

Argiri Kotsaris
  • 492
  • 3
  • 7
  • 2
    If you want order in your dict, use an OrderedDict, I asked a similar question here http://stackoverflow.com/questions/15479928/why-the-order-in-python-dictionaries-is-arbitrary –  Jan 04 '14 at 19:15
  • 2
    Dictionaries are slower to create than lists, but the difference is in nanoseconds (billions of a second). If you want to speed things up, look elsewhere. – Blender Jan 04 '14 at 19:19
  • Use `timeit.Timer` if you want to see what's faster. Especially if you've already written both versions. – Aleksander Lidtke Jan 04 '14 at 19:19
  • @EdgarAroutiounian I looked into that OrderedDict, worked like a charm I totally forgot about those. I'm also gonna look into how the hash tables are implemented. – Argiri Kotsaris Jan 04 '14 at 19:39

1 Answers1

0
  • [1, 2, 3] is a list literal. It is not list comprehension that might look like [x for x in something] (see the for-loop inside)
  • the same for dicts: {1:2, 3:4} is an ordinary dict literal. It is not dict comprehension that might look like {k:v for k, v in pairs} (again, see the for-loop inside)

My question here is why is it that when I implement the dictionary, the order of the elements seems to mix up.

The order of elements in a dictionary may change from run to run. Don't rely on it. You could use a list of pairs or collections.OrderedDict depending on your case.

It seems like the dictionary is slower to initialize all the objects than a list.

I'd expect for w in widgets to be faster if widgets is a list compared to for w in widget.values() where widgets is a dictionary. But given the code for widgets in your question; it is unlikely that it is a bottleneck in your application regardless whether you use a list or a dictionary to store a few widgets.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Yes, I had gone with the `collections.OrderedDict` and that worked nicely. Thank you for clarifying the difference between literals and comprehensions. And it wasn't a bottleneck at all, I had tried also doing `widgets = self.widgets.values()` (since this is within a class) and there was no difference. – Argiri Kotsaris Jan 04 '14 at 23:15