Questions tagged [nested-for-loop]

A logical structure used in computer programming where two or more for-loops are placed in a "nested" form (i.e., one for-loop is situated within the body of another). The inner loop is repeated in full on every pass through the outer loop.

A nested for-loop is a loop control structure which contains a (inner) loop within it, aka a loop within a loop.

Nested for-loops could be used to loop through all the elements of a multidimensional array or a matrix.

See also:

268 questions
4
votes
2 answers

What are good techniques for rewriting nested for loops in Haskell?

I'm learning Haskell and currently trying to rewrite this code: case class Coord(x: Int, y: Int) def buildBoard(coords: [Coord]): String = { var str = "" for (i <- 0 to 30) { for (j <- 0 to 40) { if (coords.exists(c => c.x…
4
votes
5 answers

Using a triple nested for loop to increase the length and count of the rows printed

I am curious as to how to code something like this: ## ## #### #### #### #### ###### ###### ###### ###### ###### ###### ######## ######## ######## ######## ######## ######## ######## ######## Here is what I have tried: public class Main { …
zmiller
  • 61
  • 3
3
votes
1 answer

How can I design a standalone C++17 std::views::join alternative?

For a C++17 restricted project I would like to have a standalone implementation of C++20 std::views::join(). I am aware of the existence of range-v3 but unfortunately the person in charge is unwilling to include further 3rd party libraries. My aim…
3
votes
3 answers

Not Another Batch File Nested For Loop Question?

I'm trying to find a way to make nested for loops work, but this iteration is different than the most popular results (where an OP is looping through directories, or using a numerical for /l loop, etc.) Instead, I'm trying to figure out how to make…
k1dfr0std
  • 379
  • 1
  • 15
3
votes
1 answer

Python: Error in nested one line for loop in static context

In nested one line for loop in static context the second loop does not accept variable (here 'l') Do I have to understand that? class T(): l = 3 t1 = [(k,v) for k in range(l) for v in range(3)] # ok t2 = [(k,v) for k in range(l) for v in…
Ren Tier
  • 31
  • 1
3
votes
3 answers

compact form of many for loop in C++

I have a piece of code as follows, and the number of for loops is determined by n which is known at compile time. Each for loop iterates over the values 0 and 1. Currently, my code looks something like this for(int in=0;in<2;in++){ for(int…
Ghoti
  • 43
  • 8
2
votes
2 answers

Method of referencing each row of 2d array with for loop in C++

I was practicing array problems and I stuck by this one: Given a declaration of 2D array: int a[][2] = { {2,2}, {3,3}, {4,4} }; write a nested for loop to print all the values of a. First, since 2D array is an array of rows (means each element of…
Nduayu
  • 25
  • 5
2
votes
1 answer

Accessing key values of nested associative array

I have a nested array like this: $settings = [ 'settings_page' => [ 'page' => 'handler_environment', 'options' => [ 'op_1' = 'val_1', 'op_2' = 'val_2', …
2
votes
1 answer

Elegant solution - Nested for loops (eslint no-await-in-loop)

I have some code that works broadly as follows: function getRegionUsers(region) { return new Promise((resolve) => { setTimeout(() => { if (region === "emea") { return resolve(["dave", "mike", "steve"]); …
Bob
  • 303
  • 1
  • 8
2
votes
1 answer

Referencing columns in .SDcols using for loop

So what I'm trying to achieve is this : Say I have a data table dt having (say) 4 columns. I want to get unique length of every combination of 2 columns. DT <- data.table(a = 1:10, b = c(1,1,1,2,2,3,4,4,5,5), c = letters[1:10], d =…
2
votes
1 answer

My python list is being printed entirely in one row in a HTML table

I am making a python script which makes a html table with a list (Without a module), but it keeps printing the entire list in one row instead of wrapping it like I need. My list is data = ['1', '2', '3', ... , '30']. Im trying to get it to 5 cells…
EhMehMan
  • 23
  • 3
2
votes
3 answers

How do I select a specific key with nested dictionaries in python?

I'm new to working with dictionaries in python and have been stuck on how I can iterate through and reference a specific key within a nested dictionary. For instance, in the following code, I'm looking to list out only the pet names for each pet in…
2
votes
2 answers

Missing one value while using in_array in php

I have two arrays and below is the output. The first array is my all the list and the second I am getting by the user selected. $specification=getgeneralSpecifications($pdo); // getting below array Array( [0] => Array ( …
user9437856
  • 2,360
  • 2
  • 33
  • 92
2
votes
3 answers

How can I have two for loops in the same list but the second one to start from the next value of the first loop?

I am trying to write a code where i have a list of vectors and Ι have to find the angle between every vector and the rest of them.(I am working on mediapipe's hand landmarks). My code so far is this one: vectors = [thumb_cmc_vec, thumb_mcp_vec,…
Lia Toutou
  • 33
  • 5
2
votes
1 answer

extract dictionary elements from nested list in python

I have a question. I have a nested list that looks like this. x= [[{'screen_name': 'BreitbartNews', 'name': 'Breitbart News', 'id': 457984599, 'id_str': '457984599', 'indices': [126, 140]}], [], [], [{'screen_name':…
Manny Kim
  • 23
  • 2
1
2 3
17 18