1

I came across an expression in one of the pyqtgraph examples which I don't quite understand.

spots = [{'pos': pos[:,i], 'data': 1} for i in range(n)] + [{'pos': [0,0], 'data': 1}]

In the example pos is a two-dimensional NumPy array and n a positive integer.

What exactly is the for statement doing in inside an expression?
Also, I might need an explanation for pos[:,i].
Lastly, how exactly does the + operator work with lists? Does it just merge them like list.extend(anotherlist)?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Parzival
  • 95
  • 6
  • 1. It's a list comprehension 2. That's a list slice 3. Yes. This should give you enough to Google. – MJeffryes Mar 30 '15 at 15:56
  • 1
    Yes to your last question, it will just merge the lists. For the for loop in the list makes it a list comprehension which you can check out here: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions. As for the comma in the list slice. that's advanced indexing in numpy, again you can read up on it here: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html – NDevox Mar 30 '15 at 15:56
  • [List comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions), [list slicing](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation), [`+` with lists](http://stackoverflow.com/questions/1720421/merge-two-lists-in-python), Google is everyone's friend. – miradulo Mar 30 '15 at 15:56
  • Thanks, that's enough to google it – Parzival Mar 30 '15 at 16:01
  • Tip for the future: multiple questions in one are frowned upon. Questions are supposed to be useful for the future too, and it's unlikely that someone will have exactly the same three questions that you do, whereas each of your individual questions has been asked and answered on SO many times before. – DSM Mar 30 '15 at 16:09

0 Answers0