Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a list of strings to an existing key in a dict

I am trying to add strings to a list(adding new strings in each loop with .append()), after that adding the list to an existing key. The problem is after adding the new string to the list and than to a key in console get printed […](how to get rid of this […]) example:

x = {}
y = ["going home"]
x["key"].append(y)
y.append("after lunch")
x["key"].append(y)
print(x)
{'key' : ['going home', 'after lunch', [...]]}

Thank you for your time

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe the correct behavior is:

x = {}
y = ["going home"]
x["key"] = y
y.append("after lunch")
print(x)
{'key': ['going home', 'after lunch']}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading