Same dict object in List of dict Python
NickName:amulllb Ask DateTime:2014-03-15T09:17:39

Same dict object in List of dict Python

Why in Python, use of n * [dict()] to create nested dictionary causes same dictionary object?

>>> d = [(111, 2222), (3333, 4444), (555, 666)]
>>> d
[(111, 2222), (3333, 4444), (555, 666)]
>>> x = len(d) * [dict().copy()][:]
>>> x
[{}, {}, {}]
>>> x[0]['x'] = 'u'
>>> x # All entries of x gets modified
[{'x': 'u'}, {'x': 'u'}, {'x': 'u'}]
>>> 
>>> x = [dict(), dict(), dict()]
>>> x
[{}, {}, {}]
>>> x[0]['x'] = 'u'
>>> x # only one entry of x gets modified
[{'x': 'u'}, {}, {}]
>>> 

Copyright Notice:Content Author:「amulllb」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/22418390/same-dict-object-in-list-of-dict-python

More about “Same dict object in List of dict Python” related questions

Same dict object in List of dict Python

Why in Python, use of n * [dict()] to create nested dictionary causes same dictionary object? >>> d = [(111, 2222), (3333, 4444), (555, 666)] >>> d [(111, 2222), (3333, 4444), (5...

Show Detail

List of dict in Python

I've got a list of dict in Python: dico_cfg = {'name': entry_name, 'ip': entry_ip, 'vendor': combo_vendor, 'stream': combo_stream} self.list_cfg.append(dico_cfg) I append to my list a lot

Show Detail

Iterate over a dict or list in Python

Just wrote some nasty code that iterates over a dict or a list in Python. I have a feeling this was not the best way to go about it. The problem is that in order to iterate over a dict, this is the

Show Detail

Python Typing List[Dict] vs List[dict]

In adding type hints to python functions which is preferred? from typing import List, Dict def example_1() -> List[Dict]: pass def example_2() -> List[dict]: pass I know that if I ...

Show Detail

create a dict of dict from dict of lists in python

I have a python object which is a dict of keys being hostnames and values being a list of users and their disk usage in the notation of dicts. I have pasted my dict below as the explanation seems

Show Detail

Append to a nested list or dict in Python

I often find myself populating lists and dicts by reading in files line by line. Let's say I'm reading in a list of people and their favourite foods: ANNE CHEESE ANNE POTATO JOE ...

Show Detail

Python Dict inside List inside Dict - comprehension possible?

I have an object in Python 3 of this format: a = { 'events': [ { 'timestamp': 123, 'message': 'test' }, {

Show Detail

Python: How to traverse a List[Dict{List[Dict{}]}]

I was just wondering if there is a simple way to do this. I have a particular structure that is parsed from a file and the output is a list of a dict of a list of a dict. Currently, I just have a...

Show Detail

Dict list comprehension in Python

I am a python newbie. I am trying to learn comprehension and I am stuck currently with the scenario. I am able to do this mutation sample_dict_list = [{'name': 'Vijay', 'age':30, 'empId': 1}, {'nam...

Show Detail

python: typecast object in dict()

I have the following code histos = dict() for key in list: obj = file.Get(key) if not key in histos: histos[key] = obj.Clone() else histos[key].Add(obj) Add() is a function of obj,

Show Detail