Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong behavior for list of lists #68777

Closed
dechamps mannequin opened this issue Jul 8, 2015 · 4 comments
Closed

Wrong behavior for list of lists #68777

dechamps mannequin opened this issue Jul 8, 2015 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@dechamps
Copy link
Mannequin

dechamps mannequin commented Jul 8, 2015

BPO 24589
Nosy @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-07-08.11:08:12.246>
created_at = <Date 2015-07-08.08:01:16.474>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'Wrong behavior for list of lists'
updated_at = <Date 2015-07-08.11:08:12.246>
user = 'https://bugs.python.org/dechamps'

bugs.python.org fields:

activity = <Date 2015-07-08.11:08:12.246>
actor = 'martin.panter'
assignee = 'none'
closed = True
closed_date = <Date 2015-07-08.11:08:12.246>
closer = 'martin.panter'
components = ['Interpreter Core']
creation = <Date 2015-07-08.08:01:16.474>
creator = 'dechamps'
dependencies = []
files = []
hgrepos = []
issue_num = 24589
keywords = []
message_count = 4.0
messages = ['246450', '246451', '246454', '246455']
nosy_count = 4.0
nosy_names = ['martin.panter', 'zorceta', 'dechamps', 'Bastiaan Albarda']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24589'
versions = ['Python 2.7']

@dechamps
Copy link
Mannequin Author

dechamps mannequin commented Jul 8, 2015

After creating a list of lists, changing one element, leads to changes of all the elements:

>>> v=[[]]*10
>>> v
[[], [], [], [], [], [], [], [], [], []]
>>> v[3].append(3)
>>> v
[[3], [3], [3], [3], [3], [3], [3], [3], [3], [3]]
>>> 
>>> v=[[]]*10
>>> v
[[], [], [], [], [], [], [], [], [], []]
>>> v[3] += [3]
>>> v
[[3], [3], [3], [3], [3], [3], [3], [3], [3], [3]]
>>>

@dechamps dechamps mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jul 8, 2015
@zorceta
Copy link
Mannequin

zorceta mannequin commented Jul 8, 2015

FYI:

>>> ll = [[]]*10
>>> [id(l) for l in ll]
[67940296, 67940296, 67940296, 67940296, 67940296, 67940296, 67940296, 67940296, 67940296, 67940296]

@BastiaanAlbarda
Copy link
Mannequin

BastiaanAlbarda mannequin commented Jul 8, 2015

The * operator lets you use the same object multiple times, thereby saving resources.

If you want unique objects use comprehension:
>>> v = [[] for x in range(10)]
>>> v[3].append(3)
>>> v
[[], [], [], [3], [], [], [], [], [], []]
>>> v[3] += [3]
>>> v
[[], [], [], [3, 3], [], [], [], [], [], []]

@vadmium
Copy link
Member

vadmium commented Jul 8, 2015

This is how Python is meant to work; see <https://docs.python.org/2.7/faq/programming.html#how-do-i-create-a-multidimensional-list\>. Unless there is something in the documentation that gave you the wrong impression, I suggest we close this.

@vadmium vadmium added the invalid label Jul 8, 2015
@vadmium vadmium closed this as completed Jul 8, 2015
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant