Python Forum
Problems with For Loops and Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with For Loops and Lists
#1
Hello all,
I am beginner and I can't find the mistake in my code.

Starting with a list list_item = [0, 0, 0] I want modify every single element sequentially.
I want also that each modified element will become an element of the list called list_with_list_items.

I wrote this code:

list_item = [0, 0, 0] 
list_with_list_items=[] 
for n0 in range(0, 5): 
    list_item [0] = n0+1 
    for n1 in range(n0+1, 6): 
        list_item [1] = n1+1 
        for n2 in range(n1+1, 7): 
            list_item [2] = n2+1 
            print(str(list_item)) 
            list_with_list_items.append(list_item) 
print(str(list_with_list_items)) 
but the output is:
[[5, 6, 7], [5, 6, 7], [5, 6, 7], …., [5, 6, 7], [5, 6, 7], [5, 6, 7]]

what I want is:
[[1, 2, 3], [1, 2, 4], [1, 2, 5], …., [4, 5, 7], [4, 6, 7], [5, 6, 7]]

but I can't find the mistake.

Thank you in advance for your help.
Reply


Messages In This Thread
Problems with For Loops and Lists - by Sagramor72 - Mar-04-2019, 10:39 AM
RE: Problems with For Loops and Lists - by scidam - Mar-04-2019, 11:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,368 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Money conversion - problems with lists and .format function fatherted99 1 1,820 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  coding help lists, loops, and if statement ilondire05 5 2,895 Jul-19-2019, 07:32 AM
Last Post: perfringo
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,264 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

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