On 15.10.2016 18:16, Steve D'Aprano wrote:
# Python 3 only: use a dict comprehension
py> d = {x:[] for x in (1, 2, 3)}
py> d
{1: [], 2: [], 3: []}
dict (and set) comprehensions got backported so this works just as well
in Python 2.7
Wolfgang
--
https://mail.python.org/mailman/listinfo/pytho
On Sat, 15 Oct 2016 11:35 pm, Uday J wrote:
> Hi,
>
> Here is the code, which I would like to understand.
>
l=['a','b','c']
bm=dict.fromkeys(l,['-1','-1'])
fromkeys() doesn't make a copy of the list each time it is used. It uses the
exact same list each time. Watch:
py> L = []
py> d
On Sun, Oct 16, 2016 at 3:12 AM, Jussi Piitulainen
wrote:
> Chris Angelico writes:
>
>> On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote:
>> bm=dict.fromkeys(l,['-1','-1'])
>>
>> When you call dict.fromkeys, it uses the same object as the key every
>> time. If you don't want that, try a dict com
Chris Angelico writes:
> On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote:
> bm=dict.fromkeys(l,['-1','-1'])
>
> When you call dict.fromkeys, it uses the same object as the key every
> time. If you don't want that, try a dict comprehension instead:
s/key/value/
--
https://mail.python.org/mailm
Uday J writes:
> Hi,
>
> Here is the code, which I would like to understand.
>
l=['a','b','c']
bm=dict.fromkeys(l,['-1','-1'])
u={'a':['Q','P']}
bm.update(u)
bm
> {'a': ['Q', 'P'], 'c': ['-1', '-1'], 'b': ['-1', '-1']}
for k in bm.keys():
> bm[k].append('DDD')
>
On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote:
bm=dict.fromkeys(l,['-1','-1'])
When you call dict.fromkeys, it uses the same object as the key every
time. If you don't want that, try a dict comprehension instead:
bm = {x: ['-1', '-1'] for x in l}
This will construct a new list for every k
Hi,
Here is the code, which I would like to understand.
>>> l=['a','b','c']
>>> bm=dict.fromkeys(l,['-1','-1'])
>>> u={'a':['Q','P']}
>>> bm.update(u)
>>> bm
{'a': ['Q', 'P'], 'c': ['-1', '-1'], 'b': ['-1', '-1']}
>>> for k in bm.keys():
bm[k].append('DDD')
>>> bm
{'a': ['Q', 'P', 'DDD'], 'c': [