python - How to instantiate multiple lists to the same value, though distinct in memory? -
i need declare 3 different lists (initially) same value:
a = b = c = ["ralph"]
but when append "a":
a.append("wiggum")
the following results generated:
print a, b, c >>["ralph", "wiggum"] ["ralph", "wiggum"] ["ralph", "wiggum"]
what need is:
print a, b, c >>["ralph", "wiggum"] ["ralph"] ["ralph"]
i know if make use of copy.copy() or instantiate new list, create new space in memory. looking short (preferably 1 line) way similar a=b=c generates distinct lists. how can this?
if want one-liner, can this:
a, b, c = (["ralph"] _ in range(3))
but frankly, if it's small list example, i'd make obvious:
a, b, c = ["ralph"], ["ralph"], ["ralph"]
Comments
Post a Comment