Try to use the zip() function:

d=[] #This is done to avoid name 'd' is not defined
arr = [[1, 2, 3, 4], [5, 6, 7, 8]]
zipped = zip(arr[1], arr[0])
for i1,i2 in zipped:
    d.append(i1/i2)
Answer from HydeNor on Stack Overflow
🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-python › lessons › exploring-the-dimensions-a-beginners-guide-to-multidimensional-arrays-in-python
A Beginner's Guide to Multidimensional Arrays in Python
Picture it as an 'apartment building' ... and how to handle them effectively in Python. To construct a multidimensional or nested array in Python, we use lists inside lists....
Discussions

Multidimensional array in Python - Stack Overflow
Mutable objects (e.g. list) can change - so initialization is not always relevant. In numpy, an Array has a Shape (dimensions) that can be modified without changing the data. 2008-11-04T06:02:50.38Z+00:00 ... Save this answer. ... Show activity on this post. Here's a quick way to create a nested ... More on stackoverflow.com
🌐 stackoverflow.com
Access elements from nested array in python - Stack Overflow
Or anything similar to that. (I only need those numbers from the whole array). More on stackoverflow.com
🌐 stackoverflow.com
nested Python numpy arrays dimension confusion - Stack Overflow
@aconkey: you say, "I need some ... what it is about this "array of pairs" that does not match what you mean by an "array of pairs"? 2015-07-18T16:44:38.627Z+00:00 ... Generally, nested NumPy arrays of NumPy arrays are not very useful.... More on stackoverflow.com
🌐 stackoverflow.com
Nested array computations in Python using numpy - Stack Overflow
This may 'feel' wasteful when coming ... of doing arrays (omg more memory consumption), but staying away from nested datastructures is almost certainly your best bet in terms of performance, and the amount of numpy/scipy ecosystem that will actually be compatible with your data representation. If it really uses more memory is actually rather questionable; every new python object uses ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › how do i make a nested array of different rows?
r/learnpython on Reddit: How do I make a nested array of different rows?
November 20, 2022 -

I want to create a 10x10 matrix of false booleans. I have done:

myMatrix = [[False] * 10] * 10

but I am finding that these rows are always the same. For example if I then do

myMatrix[0][3] = True

then every single row in my matrix will have the fourth boolean as True, which I don’t want - I want every row to be separate an unique.

Any help appreciated!

Top answer
1 of 3
2
you want to make a new copy of the list. Your code declares the inner list and then makes a list of 10 references to it. the star operator here is doing this, effectively. inner = [False] * 10 outer = [inner] * 10 so each inner list is actually a reference to the same list "inner". What you really want to do is this: outer = [] for _ in range(10): new_inner = [False] * 10 outer.append(new_inner) This creates 10 separate sublists. You can achieve this in one line like so: outer = [[False] * 10 for _ in range(10)] The way comprehension syntax works is different to the star operator as it evaluates the expression on each iteration rather than evaluating it once at the start and copying it (like star does). Remember that False and True are immutable values, so using the star operator to make a list of the inner dimensions is fine. If you were holding mutable objects (maybe an object holding a User's details or something) then you'd want to use a nested for loop to initialise new copies rather than use the star operator, for the same reason this doesn't work as intended in your example. In this case your code would become this: outer = [] for _ in range(10): inner = [] for _ in range(10): next_item = inner.append(next_item) outer.append(inner) That aside, you could also consider representing your matrix as a single dimensioned list and use arithmetic to work out your indexes. width = 10 height = 10 matrix = [False] * width * height # set element (7, 4) to true matrix[7 + (4 * width)] = True Effectively, in a 1D array representing a 2D matrix, you can say that (x, y) := matrix[x + (y * width)] Probably not much benefit here in your case, but it can be useful to remember since operations like transposing are just a case of swapping your x and y coordinates if you use this system. Hope that helps :-)
2 of 3
1
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list
🌐
Quora
quora.com › How-do-you-create-a-nested-array-in-Python
How to create a nested array in Python - Quora
Answer (1 of 7): In one sense you don't and can't. If we go by the widely accepted meaning of an array in the field f computer programming, then there's no such thing as an array nested within another. An array is a contiguous block of memory containing a homogeneous collection of data elements ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › multidimensional array in python
Multidimensional Array in Python | Creating a Multidimensional List
April 17, 2023 - Multidimensional Array concept ... (2D). In Python, Multidimensional Array can be implemented by fitting in a list function inside another list function, which is basically a nesting operation for the list functi...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GeeksforGeeks
geeksforgeeks.org › python › multi-dimensional-lists-in-python
Multi-dimensional Lists in Python - GeeksforGeeks
October 30, 2025 - In Python, a Multi-dimensional List is a list containing other lists, often used to represent structured data like matrices, tables or 2D arrays. It’s useful for storing and accessing data in rows and columns, commonly applied in data analysis, ...
🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
You can use nested generators to create two-dimensional arrays, placing the generator of the list which is a string, inside the generator of all the strings.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python – Matrices Explained with Examples
December 11, 2025 - NumPy provides a powerful N-dimensional ... how to create and work with multi-dimensional arrays in Python using NumPy. To create a multi-dimensional array using NumPy, we can use the np.array() function and pass in a nested list of values as an argument....
🌐
Learn By Example
learnbyexample.org › python-nested-list
Python Nested List - Learn By Example
June 20, 2024 - Nested lists in Python are lists that contain other lists as their elements.
🌐
Medium
soumenatta.medium.com › how-to-work-with-multidimensional-arrays-in-python-a-beginners-guide-bd437495c87
How to Work with Multidimensional Arrays in Python: A Beginner’s Guide | by Dr. Soumen Atta, Ph.D. | Medium
April 8, 2023 - Multidimensional arrays, also known as “nested arrays” or “arrays of arrays,” are an essential data structure in computer programming. In Python, multidimensional arrays can be implemented using lists, tuples, or numpy arrays.
🌐
Startcoder
startcoder.com › en › learn › python › arrays › nested-arrays
Startcoder
We see that studyGroups[0] is the element at index 0 in the studyGroups array, which is: ["Jennifer", "Boris"]. And when we want a value from this array, we just add square brackets with the index of the nested array: studyGroups[0][1]. This first selects the element at index 0 of the studyGroups array, which is ["Jennifer", "Boris"], and from this element, we select the element at index 1, which is "Boris".
🌐
AskPython
askpython.com › python › array › multidimensional-arrays
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - In this article, the creation and implementation of multidimensional arrays (2D, 3D as well as 4D arrays) have been covered along with examples in Python
🌐
Medium
mowbray-chad.medium.com › understanding-nested-list-comprehensions-in-python-6fd6be8ce8ec
Understanding Nested List Comprehensions in Python | by Chad Mowbray | Medium
December 14, 2020 - I was recently working on an NLP project and kept running into nested list comprehensions, often with conditionals.
Top answer
1 of 3
1

Numpy treats its arrays as matrices, and resource_arr is not a (valid) matrix. In your case a python list is more suitable:

def sum_nested(l):
    tmp = []

    for element in l:
        if isinstance(element, list):
            tmp.append(numpy.sum(element))
        else:
            tmp.append(element)

    return tmp

In this function we check for each element inside l if it is a list. If so, we sum its elements. On the other hand, if the encountered element is just a number, we leave it untouched. Please note that this only works for one level of nesting.

Now, if we run sum_nested([[2, 3], 4, 2, [1, 2]]) we will get [5 4 2 3]. All that's left is multiplying this result by the elements of rndm, which can be achieved easily using numpy:

def fitness_score(a, b):
    return numpy.multiply(a, sum_nested(b))
2 of 3
1

Numpy is all about the non-jagged arrays. You can do things with jagged arrays, but doing so efficiently and elegantly isnt trivial.

Almost always, trying to find a way to map your datastructure to a non-nested one, for instance, encoding the information as below, will be more flexible, and more performant.

resource_arr = (
    [0, 0, 1, 2, 3, 3]
    [2, 3, 4, 2, 1, 2]
)

That is, an integer denoting the 'row' each value belongs to, paired with an array of equal size of the values themselves.

This may 'feel' wasteful when coming from a C-style way of doing arrays (omg more memory consumption), but staying away from nested datastructures is almost certainly your best bet in terms of performance, and the amount of numpy/scipy ecosystem that will actually be compatible with your data representation. If it really uses more memory is actually rather questionable; every new python object uses a ton of bytes, so if you have only few elements per nesting, it is the more memory efficient solution too.

In this case, that would give you the following efficient solution to your problem:

output = np.bincount(*resource_arr) * rndm
🌐
GeeksforGeeks
geeksforgeeks.org › convert-python-nested-lists-to-multidimensional-numpy-arrays
Convert Python Nested Lists to Multidimensional NumPy Arrays | GeeksforGeeks
July 9, 2021 - Both lists and NumPy arrays are inter-convertible. Since NumPy is a fast (High-performance) Python library for performing mathematical operations so it is preferred to work on NumPy arrays rather than nested lists.