How to initialize a two-dimensional array in Python?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping
--
Chapters
00:00 How To Initialize A Two-Dimensional Array In Python?
00:23 Answer 1 Score 274
00:34 Accepted Answer Score 417
01:09 Answer 3 Score 154
02:13 Answer 4 Score 78
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/2397...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #multidimensionalarray
#avk47
ACCEPTED ANSWER
Score 417
A pattern that often came up in Python was
bar = []
for item in some_iterable:
    bar.append(SOME EXPRESSION)
which helped motivate the introduction of list comprehensions, which convert that snippet to
bar = [SOME_EXPRESSION for item in some_iterable]
which is shorter and sometimes clearer. Usually, you get in the habit of recognizing these and often replacing loops with comprehensions.
Your code follows this pattern twice
twod_list = []                                       \                      
for i in range (0, 10):                               \
    new = []                  \ can be replaced        } this too
    for j in range (0, 10):    } with a list          /
        new.append(foo)       / comprehension        /
    twod_list.append(new)                           /
ANSWER 2
Score 274
You can use a list comprehension:
x = [[foo for i in range(10)] for j in range(10)]
# x is now a 10x10 array of 'foo' (which can depend on i and j if you want)
ANSWER 3
Score 154
This way is faster than the nested list comprehensions
[x[:] for x in [[foo] * 10] * 10]    # for immutable foo!
Here are some python3 timings, for small and large lists
$python3 -m timeit '[x[:] for x in [[1] * 10] * 10]'
1000000 loops, best of 3: 1.55 usec per loop
$ python3 -m timeit '[[1 for i in range(10)] for j in range(10)]'
100000 loops, best of 3: 6.44 usec per loop
$ python3 -m timeit '[x[:] for x in [[1] * 1000] * 1000]'
100 loops, best of 3: 5.5 msec per loop
$ python3 -m timeit '[[1 for i in range(1000)] for j in range(1000)]'
10 loops, best of 3: 27 msec per loop
Explanation:
[[foo]*10]*10 creates a list of the same object repeated 10 times. You can't just use this, because modifying one element will modify that same element in each row!
x[:] is equivalent to list(X) but is a bit more efficient since it avoids the name lookup. Either way, it creates a shallow copy of each row, so now all the elements are independent.
All the elements are the same foo object though, so if foo is mutable, you can't use this scheme., you'd have to use
import copy
[[copy.deepcopy(foo) for x in range(10)] for y in range(10)]
or assuming a class (or function) Foo that returns foos
[[Foo() for x in range(10)] for y in range(10)]
ANSWER 4
Score 78
To initialize a two-dimensional array in Python:
a = [[0 for x in range(columns)] for y in range(rows)]