Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete from list
#1
how do I delete values from a list taking user input ?
>>> animals = [a,c] = [1,2]
>>> print(a)
1
>>> print©
2
>>> print(animals)
[1, 2]
>>> pick = input("Enter a value to delete: ")
Enter a value to delete: a
>>> animals.remove(pick)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

Also:
fish = [a,b,c,d,e,f] = [1,2,3,4,5,6]
How can delete 2 values at a time from this list using random.sample(listname,2)

Thanks
Reply
#2
Try this
animals = [a,c] = [1,2]
print(animals)
pick = input("Enter a value to delete: ")
if pick == 'a':
    animals.remove(1)
elif pick == 'b':
    animals.remove(2)
else:
    print('Invalid input')    
print(animals) 
Reply
#3
What is the reason writing this line:

>>> animals = [a,c] = [1,2]
What is wrong with simple way:

>>> lst = ['a', 'b', 'c']
>>> lst.remove('a')
>>> lst
['b', 'c']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
@Evil_Patrick: Think what will happen if it's a huge list(s) or if the list(s) changes...

@metro17: this is wrong by design. user should not be concerned with internals (i.e. they are not expected to know anything about variable names)
if you look at animals = [a,c] = [1,2] - a and c are variable names.
You should use proper data structure like dict:

animals = {'rabbit':1, 'fox':2}
print(animals)
pick = input("Enter animal to delete: ")
del animals[pick]
print(animals)
Output:
{'rabbit': 1, 'fox': 2} Enter animal to delete: fox {'rabbit': 1}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Just iterating buran code for fun:

>>> s = {'rabbit', 'fox'}
>>> s.remove(input('Enter animal to delete: '))
Obviously code is dependant on user input correctness.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
thanks Buran

import random
>>> Alldict = {'clubsacesmin':1,'clubsacesmax':11,'diamondsacesmin':1,'diamondsacesmax':11}
>>> n = 0
>>> while n < 2:
...     pick = (random.choice(list(Alldict)))
...     del Alldict(pick)
...     print(Alldict)
...
  File "<stdin>", line 3
SyntaxError: can't delete function call
I am unable to delete each randomly picket item ?
How can this be fixed
thanks

got it working.
import random
Alldict = {'clubsacesmin':1,'clubsacesmax':11,'diamondsacesmin':1,'diamondsacesmax':11}
n = 0
while n < 2:
    pick = (random.choice(list(Alldict)))
    del (Alldict[pick])
    print(Alldict)
    n = n + 1

import random
Alldict = {'clubsacesmin':1,'clubsacesmax':11,'diamondsacesmin':1,'diamondsacesmax':11}
Dealerdict = {}
Playerdict = {}
n = 0
while n < 2:
    pick = (random.choice(list(Alldict)))
    Dealerdict.update(pick.keys(),Dealerdict.update(pick.values())
    del (Alldict[pick])
    print(Alldict)
    n = n + 1 
How can add key value pair to the new dictionary here?
Reply
#7
(Nov-14-2019, 09:29 AM)metro17 Wrote: How can add key value pair to the new dictionary here?
https://python-forum.io/Thread-Basic-Dictionaries
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
how can I randomly pick a key,value pair from a dictionary?
..and then assign it to a variable and use the variable to delete from the dictionary
..and use the key value pair captured in the variable to add to another dictionary
..and use the variable to print keys and values ?
a = (random.choice(list(Alldict)))
print(a)
The above statement gives me only the key but not the value?
Reply
#9
Please, go back to basics and read about dicts and how to access value by key and how to assign value to dict key. Thede ure distinctive features of dicts
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Delete list while iterating shantanu97 1 1,866 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  I cannot delete and the elements from the list quest 4 2,921 May-11-2021, 12:01 PM
Last Post: perfringo
  How do I delete symbols in a list of strings? Than999 1 2,241 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  How to list number of times element is in defaultdict and delete it mrapple2020 3 2,617 Apr-15-2019, 07:34 AM
Last Post: perfringo
  Not Able To Delete First Node From Python Linked List ribena1980 8 4,164 Mar-05-2019, 03:14 PM
Last Post: ichabod801
  How to get a random item from a list, print it and delete it? giorgosmarga 3 2,544 Feb-22-2019, 05:33 PM
Last Post: Yoriz
  [Outlook] How to delete items from To-Do List? Winfried 1 3,343 Oct-19-2018, 09:05 PM
Last Post: nilamo
  Compare element of list with line of file : if match, delete line silfer 4 3,474 Jul-21-2018, 02:44 PM
Last Post: silfer
  How do I loop through a list and delete numerical elements that are 1 lower/higher? neko 4 4,255 Sep-05-2017, 02:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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