Search code examples
pythonlistwhile-loopscheduling

Is there a way through which I can append the new list into the existing list on each while iteration?


**Can someone please help me with a better way of doing it?

This is my output.The user has to input whether he has a new process to enter if yes then enter new process and it must get appended to a new list.But in my case after every new iteration of while it gets appended to the first list instead.


Enter process name: 1
Enter process Arrival Time: 3
Enter Execution Time: 2

[['1', 3, 2]]
Enter process name: 2
Enter process Arrival Time: 4
Enter Execution Time: 3

[['1', 3, 2], ['2', 4, 3]]
enter new processy
ENTER TOTAL NUMBER OF PROCESSES: 1
Enter process name: 3
Enter process Arrival Time: 2
Enter Execution Time: 3

[['1', 3, 2, '3', 2, 3], ['2', 4, 3], []]
enter new process

----------------------------------------------
>This is my code 

a = 0
#p = [['p1', 0, 2], ['p2', 1, 2], ['p3', 5, 3], ['p4', 6, 4]]
a2 = [0]
completion_time=[]
pp = []
tat=[]
waiting_time=[]
p = []
total_wtime = 0
n=0

def addnew():
    n = int(input('ENTER TOTAL NUMBER OF PROCESSES: '))
    for i in range(n):
        p.append([])
        p[i].append(input('Enter process name: '))
        p[i].append(int(input('Enter process Arrival Time: ')))

        p[i].append(int(input('Enter Execution Time: ')))
        print('')
        print(p)

ans="y"
while(ans=="y"):
    addnew()
    ans = input("enter new process")

Solution

  • Your index variable i is looping from 0 to n-1 every time, even when you are adding elements an existing list.

    The best approach is probably just to add new elements to a new list, and then append that, as Prune suggested. But if you are looking for the minimal change to your existing code, then change the line:

        for i in range(n):
    

    to

        for i in range(len(p), len(p) + n):
    

    And this will ensure that when you call addnew again, the indices keep going from where you left off, rather than starting at 0 again.


    If you do decide to adopt the approach of appending to a new list (see Prune's answer), then an additional change that you could consider, given that your variable i would no longer be used inside the loop, would be to call it _ instead of i, because this is conventional naming for dummy variables. This is not applicable to your existing code because you are using it to index p.


    And here is also another alternative...

    You could change your p[i] to p[-1] (i.e. the last element) wherever it occurs. Then again your loop variable is a dummy variable only, and it may as well loop from 0 to n-1 as at present, because we don't care what the values are provided that there are n iterations.

        for _ in range(n):
            p.append([])
            p[-1].append(input('Enter process name: '))
            p[-1].append(int(input('Enter process Arrival Time: ')))
            # ... etc ...
    

    Again, this is only a suggestion for minimal change from existing code. The cleanest approach is still the newlist one.