I am Making A program for malking a phonebook.
for that i am storing a list in dictionary.
Like this:

phbuk={}
def add():
    a=raw_input('Name    :')
    b=raw_input('Email   :')
    c=raw_input('Mobile  :')
    name=[a,b,c]
    d=len(phbuk)
    phbuk[d+1]=name

Now if i want to delete a particular record just by giving name then how can i do it.
Actually I am not able to understand how to access the values of list,which are the values of this "phbuk" dictionary so that i can perform a search and delete the required record.

Recommended Answers

All 7 Replies

hit the (CODE) button/tags before you post code, also your strategy for the key is awful...I don't understand why you would

do:d=len(phbuk);phbuk[d+1]=name

How about something more like: phbuk[a]=abc #"or [b,c] depending on your preference"#

Now if i want to delete a particular record just by giving name then how can i do it.

Well if your doing this text based then maybe you should have a menu with which the user chooses to either search, add, or delete, if they choose delete then they could enter the name:

dname=input('Enter a contact to delete: ')
if dname in phbuk.keys():
   del phbuk[dname]
else:
   print('That contact is not in the phonebook!')

but that's just what I would do...

now onto why I feel that your "d" variable was so poor, you're making the key more abstract than it needs to be, dictionaries are so wonderful,to me at least, because organize your data with strings, which can be words or names, which makes your question of how to search so easy, here's what I've done before:

#assuming were under the search option
search_n=input('Enter a contact name to search: ')
if search_n in phbuk.keys():       #note that I would capitalize search_n for both searching/deleting and when saving the contact
   for info in phbuk[search_n]:
      print(info)
else:
   print('Invalid Query')

again, these are just MY suggestions, this is your code, keep up the effort and keep us posted on your developments, also, don't forget those code tags!

> don't forget those code tags!

Please consider reporting such posts so that moderators can add tags to posts which can't be modified by the OP after the 30 minute time frame.

oh how do I report it to a mod? I know there's the bad post button; but that's only if the post violates the rules right?

Nope, if there is anything in a post which is worth a moderator's attention (spam, invalid code tags, flame wars etc.), feel free to use the "flag bad post" link to bring it to our attention.

well, even i thought of making keys as contact names but in that case we can not enter duplicate names as keys in dictionaries cannot be repeated,
So i assigned a unique number to each name so that even if there are two persons with the same name then also it will accept the data.
And please will you tell me how to use the tags
Like when i am posting program then use tags or for the whole program i need to use tags

> And please will you tell me how to use the tags

Whenever you post code, just make sure you enclose it in between the [code] and [/code] tags. e.g.

[code]
if __name__ == '__main__':
....do_it()

[/code]

will render like:

if __name__ == '__main__':
    do_it()

well, even i thought of making keys as contact names but in that case we can not enter duplicate names as keys in dictionaries cannot be repeated,
So i assigned a unique number to each name so that even if there are two persons with the same name then also it will accept the data.
And please will you tell me how to use the tags
Like when i am posting program then use tags or for the whole program i need to use tags

Exactly, you can force the user to specify which contact they're looking for; when they're entering the contacts name and information before you have it save you could check if it's already a key and tell the user they need to make it so the name is not a duplicate of a previous entry or that it will update/overwrite the previous one. I know as a user I wouldn't want to have to enter tony5 to get tony...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.