473,549 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check if an item exists in a dictionary. if yes, delete it, otherwise add it

1 New Member
hi, im having a little problem here.
i want to check if an item exists in a dictionary, if yes, i have to delete it. otherwise i have to add it.

Expand|Select|Wrap|Line Numbers
  1. d = {'1':2}
  2.  
  3. if '1' not in d.items():
  4.     d = {'1':2}
  5. else:
  6.     del (d['1'])
  7.  
  8. print (d)
  9.  
but it seems wrong. i only just started so help would be greatly appreciated. thanks!
Oct 18 '16 #1
2 1224
dwblas
626 Recognized Expert Contributor
Adding, changing and deleting items from a dictionary is in most basic tutorials. Tutorials are write once, read many, so people don't have to answer the same basic questions over and over https://wiki.python.org/moin/BeginnersGuide/Programmers
Oct 18 '16 #2
krec
3 New Member
I quite tryed to figure it out, im still a begginer but i've done this! Hope it helps :)

Expand|Select|Wrap|Line Numbers
  1. d = {'1':2}
  2. d2 = {'1':2}
  3. if d != d2:
  4.     d['1']= 2
  5. else:
  6.     del (d['1'])
  7. print d
Oct 22 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
13875
by: sam | last post by:
For Example, If I delete two times like the below code, the program will behave differently. is this correct? As C++ FAQ the delete checks where or not f1 has memory. How can the following program can go wrong? class foo{ // }; int main{ foo *f1 = new foo()
10
4497
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is "ENOENT", The file doesn't exist.
1
1679
by: Pete | last post by:
Hi, i'm building a datagrid that grabs a documents location out of a database and puts them in as a hyper link column. Ideally i'd love to be able to check the validity of the link and only display links to documents that actually exist. And alternatively generate a list of invalid links. Problem is that a link to an invalid document...
4
14846
by: Lucvdv | last post by:
What's the fastest/easiest/best way to determine if a certain item exists in a list? (And what's the slowest/dumbest - I suppose that's enumerating the elements and comparing each in turn in VB code?) My collection can have up to a few thousand elements (each either a GUID or an ID card number in "000-0000000-00" format) and an even...
1
2813
by: James | last post by:
vb.net 2003 i wrote a windows service that does threading. My codes are a) the service thread will read a list of machine from a text file (machines.txt). b) it then logon using the below Dim returnValue As Boolean = LogonUser(m_ntid, m_domain, m_pwd, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)
10
62814
by: Frank Rizzo | last post by:
How do I get the first item that was added to the Dictionary object. I've tried myDictionary, but it's not supported? Regards
7
3897
by: jmstur2 | last post by:
I have a table with what I consider duplicate records. Data in all columns are duplicate except for the date column, meaning that duplicate data was entered on different dates and those dates were stored along with the data. I want to delete the records except for most recent one. I can select the client ID field and the max(date field) to...
6
3754
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have to write a method that test a dictionary of this type: Dictionary<string, Dictionary<string, string>> to check if every string is not null or empty. How can I write this? Thanks in advance.
2
9800
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get the document headers, not the data. request.Credentials = System.Net.CredentialCache.DefaultCredentials; // This may throw a WebException:
1
2992
Soniad
by: Soniad | last post by:
Hi All, Is there any way in VBScript Array,where I can check if particular item is present in given array items. For Example : There is file extention I want to check. Dim StrFlExt StrFlExt = "doc" and here is array of file extensions :
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.