• Register
1 vote
23 views

Problem:

I am beginner in pyhton programing. I have a dictionary of string and int and that is:

Dictionary = {
    "Hola": 65,
    "tart" : 32,
    "star" : 34,
    "site" : 55,
    "pearl" : 88
    }

Supose I have to remove a key from this dictionary what should I do next.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

To remove an existing key from a dictionary you should use del[] method. For your code(supose you want to remove tart key) use:

Dictionary = {
    "Hola": 65,
    "tart" : 32,
    "star" : 34,
    "site" : 55,
    "pearl" : 88
    }
    if "tart" in Dictionary:
    
    del Dictionary["star"]
    
        print("Dictionary Updated :" , Dictionary)

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 41 views
41 views
Problem Hi Kodlogs, How to remove an existing key-value pair from a dictionary?
asked Oct 30, 2020 hashq 1.1k points
1 vote
1 answer 22 views
22 views
Problem: I have a dictionary that consists of: Dict = {     "Krishna" : 79,     "Arnav" : 46,     "Paulo" : 90,     "Christian" : 77,     "Renartovic" : 33,     "Wanda" : 65,     "Arteta" : 80,     "Santiago" : 68,     "Jovic" : 88     } I ... compiling this program:   File "val.py", line 13     if "Wanda" in Dict:     ^ IndentationError: unexpected indent Can anyone help me to solve this error?
asked Jun 7, 2020 samhaz 5k points
0 votes
1 answer 24 views
24 views
I am new to python programming I was trying to check if a letter is uppercase in python. I wrote some code to check this statement : import string def UpperCase(s_str): for x in s_str: if x not in string.UpeerCase: return False return True But I can not get the actual ... last): File "<stdin>", line 1, in <module> NameError: name 'LoWWE' is not defined Can anyone help me to solve this thing?
asked Mar 14, 2020 LizzyM 6.1k points
1 vote
1 answer 30 views
30 views
Problem: I am starting writing code in python. I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "Present" else: print "Thanks" If there any other way to check this, please let me know.
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 10 views
10 views
Problem: I have a dictionary and I would like to get some values from it based on some keys. For example, I have a dictionary for users with their first name, last name, username, address, age and so on. Let's say, I only want to get one value (name) - either last ... if key exists, get value and stop checking. If not, move to next key. (3) username: if key exists, get value or return null/empty
asked 4 days ago Mashhoodch 9.1k points
0 votes
2 answers 7 views
7 views
Problem: I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "blah" else: print "boo" I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary?
asked Feb 15 Mashhoodch 9.1k points
0 votes
1 answer 20 views
20 views
Problem Hi Kodlogs, How to check if a key is in a dictionary python? Please help Thanks
asked Oct 30, 2020 hashq 1.1k points
1 vote
1 answer 18 views
18 views
Problem: I wrote a python program to check whether a given key already exists in a dictionary. The dictionary I have is: (If exists, print Key is present and the value of the key. Otherwise print key is not present .) {'a': 233, 'b':588, 'c':967 ... checkKey() missing 1 required positional argument: 'key' Whats the problem with my program can any one help me to sort this out? Thanks in advance.
asked Apr 26, 2020 samhaz 5k points
1 vote
1 answer 47 views
47 views
Problem : I was trying to check if key exists in dictionary python. So I wrote this code: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False ... unindent does not match any outer indentation level What is the problem in this program and how can I check if key exists in dictionary?
asked Apr 26, 2020 samhaz 5k points
1 vote
1 answer 27 views
27 views
Problem: I tried to check my dictionares using if the dictionary is in use or not. To check that I wrote something like: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) But the program is not working properly what should I do?
asked Apr 3, 2020 LizzyM 6.1k points