Python Forum
Problem with append list in loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with append list in loop
#1
Here is my code

import math
import numpy as np

z = 0
y= []
x = []

while z < 5:
    z = z + 1
    t = 0
    x.append([y])
    while t < 8:
        A = 10 + t
        y.append(A)
        t = t + z
print (x)

I want to separate the result of y list in each cycle from the second while loop 

I expect the result will be 
[10,11,12,13,14,15,16,17],
[10,12,14,16],[10,13,16],
[10,14],
[10,15]
[10,16]

but the outcome is 
[10,11,12,13,14,15,16,17,10,12,14,16,10,13,16,10,14,10,15,10,16
[10,11,12,13,14,15,16,17,10,12,14,16,10,13,16,10,14,10,15,10,16],
[10,11,12,13,14,15,16,17,10,12,14,16,10,13,16,10,14,10,15,10,16],
[10,11,12,13,14,15,16,17,10,12,14,16,10,13,16,10,14,10,15,10,16],
[10,11,12,13,14,15,16,17,10,12,14,16,10,13,16,10,14,10,15,10,16]
can anyone help me? Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 463 Mar-14-2024, 06:26 PM
Last Post: flash77
  While Loop Problem Benno2805 1 570 Sep-06-2023, 04:51 PM
Last Post: deanhystad
Question How to append integers from file to list? Milan 8 1,444 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  Big O runtime nested for loop and append yarinsh 4 1,374 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  read a text file, find all integers, append to list oldtrafford 12 3,515 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 10,344 Jun-12-2022, 06:54 PM
Last Post: Mark17
  Problem in formulating append logic shantanu97 1 1,023 Jun-07-2022, 08:35 AM
Last Post: ibreeden
  Problem with "Number List" problem on HackerRank Pnerd 5 2,086 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,007 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Loop reading csv file problem faustineaiden 1 1,564 Dec-11-2021, 08:40 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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