Python Forum
Using nested for loop with a single list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using nested for loop with a single list
#1
I am trying to examine every sum of three numbers from a single list

So for example if the list was 1,2 I want to examine 1=1+1+1 and 1+1+2 and 1+2+2 and 2+2+2

In the code below:

The variable I am examining is threesum

The list is three_digit_squares[]

Whenever I run the code it gives me "index out of range" error on the line starting "threesum ="

three_digit_squares = []

for i in range (1,4):
    three_digit_squares.extend([i])
    
for i in three_digit_squares:
    for j in three_digit_squares:
        for k in three_digit_squares:
            threesum = three_digit_squares[i] + three_digit_squares[j] + three_digit_squares[k]
            if threesum == 4:
            
                print "yes"
Reply
#2
three_digit_squares is [1, 2, 3]. When it gets to three it gives an index error, because x[3] is the fourth item in the list (Python is 0 indexed).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks Craig

I realised what I'd done wrong halfway round a bike ride, you'd already replied by then.
It was rookie error from someone more used to basic and fortran, I forgot that i is the value not the index in the form of code I gave.

So changing my code as little as possible, this works OK

three_digit_squares = []

for i in range (1,4):
three_digit_squares.extend([i])

a=len(three_digit_squares)-1
for i in range(a):
for j in range(a):
for k in range(a):
threesum = three_digit_squares[i] + three_digit_squares[j] + three_digit_squares[k]
if threesum == 4:

print "yes"

I did initially try putting the len(three_digit_squares)-1 inside the range statement, but that did not work. So I had to assign it to a and use a in the range statements. It seems Python is not as infinitely substitutional as say C or C-shell? Or have I misunderstood that too!
Reply
#4
Please use python tags to preserve the indentation of your code. See the BBCode link in my signature for details.

That's a bad way to do the loop, I should have mentioned that in my first response. In Python you don't want to be looping over the indexes, you want to loop over the list:

for i in three_digit_squares:
    for j in three_digit_squares:
        for k in three_digit_squares:
            if i + j + k == 4:
                print 'yes'
Furthermore, check out the itertools module. It has several combinatoric generators that simplify things like this:

from itertools import product
...
for i, j, k in product(three_digit_squares, repeat = 3):
    if i + k + k == 4:
        print 'yes'
Also, if you are just learning Python, I would suggest learning Python 3.x instead of Python 2.x.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 914 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Big O runtime nested for loop and append yarinsh 4 1,373 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,569 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,274 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,893 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,114 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,343 Apr-11-2021, 11:03 PM
Last Post: lastyle
  convert List with dictionaries to a single dictionary iamaghost 3 2,851 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How do I add another loop to my nested loop greenpine 11 4,535 Jan-12-2021, 04:41 PM
Last Post: greenpine
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,315 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020