d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Question
Add Reply New Topic New Poll
Member
Posts: 30,905
Joined: Feb 18 2007
Gold: 0.00
Sep 24 2015 05:46pm
How can I delete a list in a list, such as:
I create a list of lists
Code
myList = []



and use the .append to add to the list, so that after user adds a few items like:
Code
[[a,b], [d,c], [e,f]]



How could I get it to delete just one of those lists after finding said list, such as:
Code
for itemOffer in range(0, len(myList)):
if myList[itemOffer] >= itemCost:
print "Sold", itemType, "for", itemCost


So how would I get what I found deleted?

This post was edited by jro1221 on Sep 24 2015 05:47pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Sep 24 2015 06:05pm
Probably a few ways, as always, you will want to use the right way.

pop I believe is what you're looking for, there is also remove and you could manually use del list[0] at the index but that's dirty.

https://docs.python.org/2/tutorial/datastructures.html

Member
Posts: 30,905
Joined: Feb 18 2007
Gold: 0.00
Sep 24 2015 07:01pm
Code
elif itemType == "d":
itemType = "dresser"
itemOffer = input("Enter the maximum item cost:")
for itemOffer in range(0, len(myList)):
if myList[itemOffer] >= itemCost:
print "Sold", itemType, "for", itemCost
myList.remove([itemType, itemCost])
dList.remove([itemType, itemCost])
break



That is what I currently have, but I am running into 2 issues:

Lets say I have [[dresser, 15], [dresser, 17]] and I put in a value of 16 for item offer
It will print:
"Sold dresser for 17"

Then it will not delete anything.

When I want it to print:
"Sold dresser for 15"






Edit: Got it to delete now, but it is still finding and deleting the wrong item.

This post was edited by jro1221 on Sep 24 2015 07:15pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Sep 25 2015 02:17am
Quote (jro1221 @ Sep 24 2015 07:01pm)
Code
elif itemType == "d":
itemType = "dresser"
itemOffer = input("Enter the maximum item cost:")
for itemOffer in range(0, len(myList)):
if myList[itemOffer] >= itemCost:
print "Sold", itemType, "for", itemCost
myList.remove([itemType, itemCost])
dList.remove([itemType, itemCost])
break



That is what I currently have, but I am running into 2 issues:

Lets say I have [[dresser, 15], [dresser, 17]] and I put in a value of 16 for item offer
It will print:
"Sold dresser for 17"

Then it will not delete anything.

When I want it to print:
"Sold dresser for 15"






Edit: Got it to delete now, but it is still finding and deleting the wrong item.


Code
>>> dressers = [[dresser, 15], [dresser, 17]]
>>> dressers.append([dresser, 16])
>>> sorted(dressers)
[[dressers , 15], [dressers , 16], [dressers , 17]]
>>> dressers.pop(2)
['', 16]
>>> dressers
[['', 15], ['', 17]]


pop would return the values from the index you need them from and also remove them in same action, which might be useful for what you need rather than manually calling remove.

I would honestly just use dictionaries, you could create {key:value} objects quickly with lists formatted like these just using dict(zip(list))

This post was edited by j0ltk0la on Sep 25 2015 02:24am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll