Python Forum
Multi-pop using different loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi-pop using different loops
#1
I'm doing a multi-pop function as following. The goal is to remove the first few elements in a list. For instance, if I have [1,2,3,4,5,6,7,8,9] and set the iter as 3. A list as [4,5,6,7,8,9] are expected. This goal is achieved by the second one, but the first one using "while" loops returns extra iterations. Can come one let me know the reason?

def MultiPop_1(l, iter):
    while iter > 0:
        l.pop(0)
        iter -= 1
        if iter == 0:
            break
        MultiPop(l,iter)
def MultiPop_2(l, iter):
    if iter > 0:
        l.pop(0)
        iter -= 1
        MultiPop(l,iter)
Reply
#2
This is already built into python, called a slice:
>>> zz = [1,2,3,4,5,6,7,8,9]
>>> zz[3:]
[4, 5, 6, 7, 8, 9]
>>>
or both ends:
>>> zz = [1,2,3,4,5,6,7,8,9]
>>> zz[3:6]
[4, 5, 6]
>>>
Reply
#3
In your first one you are doing the pops in a loop, but you are also making a recursive call to the MultiPop to do additional pops. The recursive call is unnecessary.

In your second one rather than looping, you are making a recursive call to do the rest of the pops. This works for smaller numbers, but is inefficient. Much better would be a non-recursive loop.
leoahum likes this post
Reply
#4
I'm not sure what your question is. I tried this and it works as I expect.
def MultiPop(l, iter):
    while iter > 0:
        l.pop(0)
        iter -= 1

test = list(range(10))
MultiPop(test, 3)
print(test)
Output:
[3, 4, 5, 6, 7, 8, 9]
It would be a good idea to test if you have too many pops, or is it ok if this raises an IndexError?

I agree with Larz60+, this function looks like a poor replacement for slices. You could actually implement your function using clices.
def MultiPop(l, count):
    l[:] = l[count:]

test = list(range(10))

MultiPop(test, 3)
print(test)
A few nags.

"iter" is a built-in function. Don't use it as a variable name. Instead of "iter" use "count" which I think is more meaningful anyway.

Lowercase L and uppercase o should never be used as variable names. You should avoid single letter variable names in general, but with most fonts it is hard to see the difference between lower case L (l) and one (1). The same goes for upper case o (O) and zero (0).

How many built-in functions do you know of that start with an upper case letter or use Pascal or camel case names (MultiPop or multiPop). Function names are lower case and may contain an underscore if they are made up of multiple words (multipop or multi_pop).
def multipop(alist, count=1):
    count = min(count, len(alist))
    while count > 0:
        alist.pop(0)
        count -= 1
    return alist

test = list(range(10))
print(multipop(test, 3))
leoahum likes this post
Reply
#5
Thanks Larz60+. I know slice, but my case needs me to get rid of few elements in a certain sequence.

(Jan-12-2021, 03:48 PM)Larz60+ Wrote: This is already built into python, called a slice:
>>> zz = [1,2,3,4,5,6,7,8,9]
>>> zz[3:]
[4, 5, 6, 7, 8, 9]
>>>
or both ends:
>>> zz = [1,2,3,4,5,6,7,8,9]
>>> zz[3:6]
[4, 5, 6]
>>>
Reply
#6
Thank you for the naming advises. They are helpful.

Unfortunately, using slice will not work in my case. The "count" parameter needs to be the quantity of elements pulling out from the rest of the list (That's why I take pop()). In your script with slice,it returns the same result, but the list doesn't change.


(Jan-12-2021, 08:16 PM)deanhystad Wrote: I'm not sure what your question is. I tried this and it works as I expect.
def MultiPop(l, iter):
    while iter > 0:
        l.pop(0)
        iter -= 1

test = list(range(10))
MultiPop(test, 3)
print(test)
Output:
[3, 4, 5, 6, 7, 8, 9]
It would be a good idea to test if you have too many pops, or is it ok if this raises an IndexError?

I agree with Larz60+, this function looks like a poor replacement for slices. You could actually implement your function using clices.
def MultiPop(l, count):
    l[:] = l[count:]

test = list(range(10))

MultiPop(test, 3)
print(test)
A few nags.

"iter" is a built-in function. Don't use it as a variable name. Instead of "iter" use "count" which I think is more meaningful anyway.

Lowercase L and uppercase o should never be used as variable names. You should avoid single letter variable names in general, but with most fonts it is hard to see the difference between lower case L (l) and one (1). The same goes for upper case o (O) and zero (0).

How many built-in functions do you know of that start with an upper case letter or use Pascal or camel case names (MultiPop or multiPop). Function names are lower case and may contain an underscore if they are made up of multiple words (multipop or multi_pop).
def multipop(alist, count=1):
    count = min(count, len(alist))
    while count > 0:
        alist.pop(0)
        count -= 1
    return alist

test = list(range(10))
print(multipop(test, 3))
Reply
#7
This is a perfect answer to my question. Thanks a lot!

(Jan-12-2021, 05:27 PM)bowlofred Wrote: In your first one you are doing the pops in a loop, but you are also making a recursive call to the MultiPop to do additional pops. The recursive call is unnecessary.

In your second one rather than looping, you are making a recursive call to do the rest of the pops. This works for smaller numbers, but is inefficient. Much better would be a non-recursive loop.
Reply


Forum Jump:

User Panel Messages

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