I am trying to remove a whole index in a list of lists (John in this case), as seen here where the list is called player_list
:
player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15],
['Jessica Jones', 12, 0, 6, 6, 10, 6],
['Johnny Rose', 6, 2, 0, 4, 20, 10],
['Gina Linetti', 7, 4, 0, 3, 300, 15],
['Buster Bluth', 3, 0, 2, 1, 50, 1],
['John', 0, 0, 0, 0, 100, 0]]
However, when I run the function remove_player
:
def remove_player(player_list, name):
check = find_player(player_list, name)
new_list = []
if check == -1:
print(name, "is not found in players")
else:
for i in range(len(player_list)):
if player_list[i] != player_list[check]:
new_list.append(player_list[i])
player_list = new_list
print(name, "has sucessfully been removed.")
and the function: find_player
:
def find_player(player_list, name):
found = -1
index = 0
while index < len(player_list) and found == -1:
if name == player_list[index][0]:
found = index
index = 1
return found
Where name
comes from:
removed_player = input("Please enter name: ")
remove = remove_player(player_list, removed_player)
It does remove the index from the list, however it still is printed in the table I have created here which is not what I want it to do:
===========================================================
- Player Summary -
===========================================================
- P W L D Chips Score -
===========================================================
- Bruce Wayne 5 5 0 0 100 15 -
-----------------------------------------------------------
- Jessica Jones 12 0 6 6 10 6 -
-----------------------------------------------------------
- Johnny Rose 6 2 0 4 20 10 -
-----------------------------------------------------------
- Gina Linetti 7 4 0 3 300 15 -
-----------------------------------------------------------
- Buster Bluth 3 0 2 1 50 1 -
-----------------------------------------------------------
- John 0 0 0 0 100 0 -
-----------------------------------------------------------
===========================================================
The function I have created for the table, display_players(player_list)
:
def display_players(player_list):
print("===========================================================")
print("-", format("Player Summary", ">34s"), format("-", ">22s"))
print("===========================================================")
print("-", format("P", ">28s"), format("W", ">2s"), format("L", ">2s"), format("D", ">2s"), format("Chips", ">8s"), format("Score", ">8s"), format("-", ">1s"))
print("===========================================================")
for i in range(len(player_list)):
print("-", format(str(player_list[i][0]), "<25s"), format(str(player_list[i][1]), ">2s"), format(str(player_list[i][2]), ">2s"), format(str(player_list[i][3]), ">2s"), format(str(player_list[i][4]), ">2s"), format(str(player_list[i][5]), ">8s"), format(str(player_list[i][6]), ">8s"), ("-"))
print("-----------------------------------------------------------")
print("===========================================================")
I'm not sure why it is not working. Please note I am not allowed to use any of the list functions aside from list_name.append(item)
in the remove_player function.
Thank you!
CodePudding user response:
Can you try out this?
player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15],
['Jessica Jones', 12, 0, 6, 6, 10, 6],
['Johnny Rose', 6, 2, 0, 4, 20, 10],
['Gina Linetti', 7, 4, 0, 3, 300, 15],
['Buster Bluth', 3, 0, 2, 1, 50, 1],
['John', 0, 0, 0, 0, 100, 0]]
def remove_player(player_list, name):
return [ player for player in player_list if player[0] != name]
player_list = remove_player(player_list, 'Bruce Wayne')
CodePudding user response:
You can try to use the function pop which is going to delete the specific index.
check = find_player(player_list, name)
if check == -1:
print(name, "is not found in players")
else:
player_list.pop(check)
print(name, "has sucessfully been removed.")
Here the doc
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
CodePudding user response:
Your code is except a little detail working fine. You never change the original player_list
. You use it in your function to make new_list
and then you assign player_list
to it, but all this is inside the function and you don't return it to get access to it outside of your function.
Here is your remove_player function:
def remove_player(player_list, name):
check = find_player(player_list, name)
new_list = []
if check == -1:
print(name, "is not found in players")
else:
for i in range(len(player_list)):
if player_list[i] != player_list[check]:
new_list.append(player_list[i])
player_list = new_list
print(name, "has sucessfully been removed.")
return player_list
I only added the return
statement at the end.
Now when calling this:
removed_player = input("Please enter name: ")
player_list = remove_player(player_list, removed_player)
assign the returned list to player_list
and you will see, when printing it, it works.