1

I'm just starting out with Python and need some help. I have the following list of dictionaries that contain a list:

>>> series
[{'data': [2, 4, 6, 8], 'name': 'abc'}, {'data': [5, 6, 7, 8], 'name': 'efg'}]
>>> 

How can I multiply each elements of inner lists by a constant without using loops and do it in place.

So if I have:

>>> x = 100

The code should result in:

>>> series
[{'data': [200, 400, 600, 800], 'name': 'abc'}, {'data': [500, 600, 700, 800], 'name': 'efg'}]

The best I could come up with my limited knowledge is this (and I don't even know what "[:]" is):

>>> for s in series:
...     s['data'][:] = [j*x for j in s['data']]
... 
>>> 

How can I remove the for loop?

An explanation of the code or a pointer to docs would also be nice.

Thanks!

Mat
  • 202,337
  • 40
  • 393
  • 406
Alex
  • 441
  • 5
  • 6
  • 2
    Why do you want/need to remove the `for` loop? – Amber Jul 01 '12 at 07:19
  • You technically have 2 for loops here (one in the list comp). I don't think it's possible to remove both, but you might be able to remove one. – mVChr Jul 01 '12 at 07:29

3 Answers3

0

How can I remove the for loop?

You can remove the loop with a combination of list and dict comprehensions:

>>> [{k:[j*100 for j in v] if k == 'data' else v for k,v in d.items()}
     for d in series]
[{'data': [200, 400, 600, 800], 'name': 'abc'}, {'data': [500, 600, 700, 800], 'name': 'efg'}]

However this less readable and no longer does the multiplication in-place.

So I think you shouldn't attempt to remove the loop. You should instead continue to do what you are already doing.

An explanation of the code or a pointer to docs would also be nice.

I don't think it makes much sense to explain the above code, since we've already established that it isn't what you are looking for. But perhaps you would like to know what the [:] does in your existing code. For that, I refer you to this related question, that explains it in details:

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Well, the OP did say they wanted the modification done in-place (which if something else is holding a reference to the original list, might be desirable). So the `[:]` does serve a purpose. – Amber Jul 01 '12 at 07:42
  • @Amber: Oh thanks, I didn't see that. I suspect that my answer can't be easily modified to perform the operation in place. – Mark Byers Jul 01 '12 at 07:45
0

You can remove the for loop in the list comprehension by using map:

for s in series:
    s['data'] = map(lambda d: d*x, s['data'])
mVChr
  • 49,587
  • 11
  • 107
  • 104
0

Mapping the multiplier by going through each element in the list:

series = [{'data': [2, 4, 6, 8], 'name': 'abc'}, {'data': [5, 6, 7, 8], 'name': 'efg'}]
x=100

def mult_elements(series, x):
    for s in series:
        s['data']= map(lambda y:y*x, s['data'])
    return series

print mult_elements(series, x)

Doing it as a one liner with list comprehension:

series = [{'data': map(lambda y:y*x, s['data']), 'name':s['name']} for s in series]
Bharat
  • 2,960
  • 2
  • 38
  • 57