Unable to remove all items from a list using remove() function


#1
backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
for item in backpack:
 backpack.remove(item)
print backpack

i have written this code so that all items get removed from my list.But the output is showing:
[‘dagger’,‘bread loaf’]
the length becomes 2 .please explain


#2

lists can’t have empty indexes, so lets say we have:

a_list = ['a', 'b', 'c']

the list has the following values + indexes:

a   b   c
0   1   2

and we do:

a_list.remove('a')

a gets removed, however, lists can’t have empty indexes, so the new list is:

a_list = ['b', 'c']

with b at index 0, and c at index 1

if you do the removal with a loop, the loop will skip through items because of this index shift that occurs


#3

i get it,so when i remove the first item,the second item index becomes [0] but the forloop is already at index[1] and removes ‘tent’ because of which breadloaf index is now [1] but for loop is at 2 so it doesnot remove these two items and so the output has these two items.Thank you so much.So is there any way in which i can remove all items and make the list empty ? list we do have empty list=[] right ?So how to do that ?


#4

just simply make the list empty:

backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
backpack = []

then you have one operation, not multiple removals.


#5

@stetim94 has demonstrated a convenient technique for creating an empty list.

This creates a new empty list, and assigns it to backpack

backpack = []

The remove method alters the list from which it called, rather than create a new list. The distinction may not matter for this exercise, but for other situations, we should be aware of when we are modifying an existing list and when we are creating a new list.

As a demonstration, try out the following code in Python 2, which is what Codecademy offers. The comments explain what is happening.

# Assign a list to backpack
backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
# Make knapsack refer to the same list as backpack
knapsack = backpack
# Display it
print backpack
print knapsack
# Remove 'dagger' from backpack
backpack.remove('dagger')
# Since both variables refer to the same list, we can display the value of both variables, and see that 'dagger' is gone
print backpack
print knapsack
# Assign an entirely new empty list to backpack
backpack = []
# Display the value of both variables, and see that they now refer to different lists
print backpack
print knapsack
# Start all over
backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
knapsack = backpack
# This time, empty out backpack without creating a new empty list
backpack[:] = []
# Display the value of both variables, and see that they still refer to the same list, which is now empty
print backpack
print knapsack

Output …

['xylophone', 'dagger', 'tent', 'bread loaf']
['xylophone', 'dagger', 'tent', 'bread loaf']
['xylophone', 'tent', 'bread loaf']
['xylophone', 'tent', 'bread loaf']
[]
['xylophone', 'tent', 'bread loaf']
[]
[]

A distinction to note is that this replaces the original list with an empty one …

backpack = []

… while this uses a slice to empty the original list

backpack[:] = []

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.