This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 2D array issue
Type: behavior Stage: resolved
Components: Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, ritviksetty
Priority: normal Keywords:

Created on 2021-12-16 05:16 by ritviksetty, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
2D-Array-Problem.py ritviksetty, 2021-12-16 05:16 Python File
Messages (2)
msg408671 - (view) Author: Ritvik S (ritviksetty) Date: 2021-12-16 05:16
I had a problem running the following code. When I ran through what I thought python was supposed to do, I got a different answer than what python did. I think this is an error. Here is the code:



problem_ary = [['a','b','c'],['d','e','f'],['g','h','i']]
normal_ary = ['a','b','c']

print(normal_ary[:]) #should print ['a','b','c']
print(problem_ary[:][1])



The second output should be ['b','e','h'] since the print statement tells python to take [0][1],[1][1], and [2][1] from the problem_ary which is 'b','e','h'. It confused me when python instead returned ['d','e','f']. I came across this problem when I was trying to create tic-tac-toe in python. I tried coding this is Python 3.8, 3.9, and using an online interpreter, and I got the same result every time.
msg408675 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-12-16 07:02
problem_ary[:] creates a copy of problem_ary, so it's equal to:

>>> problem_ary[:]
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

The [1] element of that is:

>>> problem_ary[:][1]
['d', 'e', 'f']

So this is working as expected.

I suggest you ask on StackOverflow or the python-list mailing list if you need more help in understanding how lists work in Python.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90251
2021-12-16 07:02:09eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg408675

resolution: not a bug
stage: resolved
2021-12-16 05:16:24ritviksettycreate