Comprehensions are a really useful feature of Python that aren't available in JavaScript (or many languages).
These concepts of course can be tranlsated into using map
instead. But especially the dictionaries are a bit
trickier.
>>> foobar = range(5)
>>> [x + 1 for x in foobar]
[1, 2, 3, 4, 5]
# Intermediate translation
>>> map(lambda x: x + 1, foobar)
[1, 2, 3, 4, 5]
becomes:
> foobar = [0, 5, 3, 2]
[ 0, 5, 3, 2 ]
> foobar.map(x => x+1)
[ 1, 6, 4, 3 ]
foobar = [{'key': 'fasdf', 'value': 'asfasdfaf'}, {'key': 'fassdfsddf', 'value': 'asdfaf'}]
>>> {x['key']: x['value'] for x in foobar}
{'fasdf': 'asfasdfaf', 'fassdfsddf': 'asdfaf'}
>>> # Intermediate translation to map
... dict(map(lambda x: (x['key'], x['value']), foobar))
{'fasdf': 'asfasdfaf', 'fassdfsddf': 'asdfaf'}
becomes:
> foobar = [{'key': 'fasdf', 'value': 'asfasdfaf'}, {'key': 'fassdfsddf', 'value': 'asdfaf'}]
[ { key: 'fasdf', value: 'asfasdfaf' },
{ key: 'fassdfsddf', value: 'asdfaf' } ]
> Object.fromEntries(foobar.map(x => [x.key, x.value]))
{ fasdf: 'asfasdfaf', fassdfsddf: 'asdfaf' }
>>> foobar = {'fasdf': 'asfasdfaf', 'fassdfsddf': 'asdfaf'}
>>> {key: val.upper() for key, val in foobar.items()}
{'fasdf': 'ASFASDFAF', 'fassdfsddf': 'ASDFAF'}
becomes:
foobar = {fasdf: 'asfasdfaf', fassdfsddf: 'asdfaf'}
Object.fromEntries(Object.entries(foobar).map(([key, value]) => [key, value.toUpperCase()]))
{ fasdf: 'ASFASDFAF', fassdfsddf: 'ASDFAF' }
Dope, Thanks again. Will surely take a look.