Home > Enterprise >  Python pop() method not returning expected values from the list
Python pop() method not returning expected values from the list

Time:11-29

I am just learning python and was trying to loop through a list and use pop() to remove all items from original list and add them to a new list formatted. Here is the code I use:

languages = ['russian', 'english', 'spanish', 'french', 'korean']
formatted_languages = []

for a in languages:
    formatted_languages.append(languages.pop().title())

print(formatted_languages)

The result is this : ['Korean', 'French', 'Spanish']

I do not understand why do I only get 3 out of 5 items in the list returned. Can someone please explain?

Thank you, Ruslan

CodePudding user response:

Look:

languages = ['russian', 'english', 'spanish', 'french', 'korean']
formatted_languages = []

for a in languages:
    print(a)
    print(languages)
    formatted_languages.append(languages.pop().title())
    
    print(formatted_languages)

Notice what "a" is:

russian
['russian', 'english', 'spanish', 'french', 'korean']
['Korean']
english
['russian', 'english', 'spanish', 'french']
['Korean', 'French']
spanish
['russian', 'english', 'spanish']
['Korean', 'French', 'Spanish']

So A is moving ahead and then it cannot continue in the loop

CodePudding user response:

What you're doing here is changing the size of the list whilst the list is being processed, so that once you've got to spanish in the list (3rd item), you've removed french and korean already, so once you've removed spanish, there are no items left to loop through. You can use the code below to get around this.

languages = ['russian', 'english', 'spanish', 'french', 'korean']
formatted_languages = []

for a in len(range(languages)):
    formatted_languages.append(languages.pop().title())

print(formatted_languages)

This iterates through the length of the list instead, which is only calculated once and therefore doesn't change

CodePudding user response:

The reason you dont get the full values is because you are making the list shorter on each iteration.

Consider what is happening in your loop:

for a in languages:
    formatted_languages.append(languages.pop().title())

By the time a is equal to the 3rd element in your list, you already removed 3 elements from it(including the current value of a), so the loop cant get next value from the list and ends.

If all you need to do is to capitalize all the strings in the list, you can just do:
formatted_languages = [entry.title() for entry in languages]

You can also set the original list to [entry.title() for entry in languages] since you are poping the values from it anyway:

languages = [entry.title() for entry in languages]

If you preffere to use a for loop similar to what you have, you should utilize the a value in your loop:

for a in languages:
    formatted_languages.append(a.title())

I will point out that some of the comments and answers here suggested to make a copy of the list.
While this will work, it is not optimal since you waste memory by doing this and if the list is very large that might be impactful.

  • Related