Python Forum
creating an 'adress book' in python using dictionaries?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating an 'adress book' in python using dictionaries?
#4
Dictionary keys must be unique, so you can't have two contacts with same nickname. It's quite probable that you will face the situation when you want to make entry but can't because nickname is already 'taken'.

One way to overcome this is to use list of dictionaries (or list on named tuples). This way there will be no collision of nicknames:

contacts = [{"nickname": "Don", "name": "Donald Jones", "address": "1 Rue Rivoli Paris ", "phone": "9444444411"}, 
            {"nickname": "Joseph", "name": "Joseph Boy", "address": "3 Tivoli Paris", "phone": "0800838383"},
            {"nickname": "Bilbo", "name": "Bilbo Baggin", "address": "4 White House Washington", "phone": "08055550838383"}]
One can write some utility functions, for example adding contacts (note, that these are required keyword arguments):

def add_contact(*, nickname, name, address, phone):
    contacts.append({"nickname": nickname, "name": name, "address": address, "phone": phone})
Search function can be also handy (enables to search with exact match or partial match):

def find_contact(*, field, value, exact=True):
    return [row for row in contacts if (value in row[field], row[field] == value)[exact]]
Some examples:

>>> add_contact(nickname='Monkey-man', name='Arthur Dent', address='Cottington, West Country, Earth', phone='42')
>>> contacts
[{'nickname': 'Don', 'name': 'Donald Jones', 'address': '1 Rue Rivoli Paris ', 'phone': '9444444411'},
{'nickname': 'Joseph', 'name': 'Joseph Boy', 'address': '3 Tivoli Paris', 'phone': '0800838383'}, 
{'nickname': 'Bilbo', 'name': 'Bilbo Baggin', 'address': '4 White House Washington', 'phone': '08055550838383'}, 
{'nickname': 'Monkey-man', 'name': 'Arthur Dent', 'address': 'Cottington, West Country, Earth', 'phone': '42'}]
>>> find_contact(field='nickname', value='Don')
[{'nickname': 'Don', 'name': 'Donald Jones', 'address': '1 Rue Rivoli Paris ', 'phone': '9444444411'}]
>>> find_contact(field='name', value='Jo', exact=False)
[{'nickname': 'Don', 'name': 'Donald Jones', 'address': '1 Rue Rivoli Paris ', 'phone': '9444444411'}, 
{'nickname': 'Joseph', 'name': 'Joseph Boy', 'address': '3 Tivoli Paris', 'phone': '0800838383'}]
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


Messages In This Thread
RE: creating an 'adress book' in python using dictionaries? - by perfringo - May-04-2019, 07:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner stuck in Python book for kids mic81k 11 1,608 Nov-27-2023, 04:28 AM
Last Post: deanhystad
  Deitel book "Python for Programmers" ricardian 7 28,003 May-12-2023, 01:33 PM
Last Post: snippsat
  best " Learning Python " book for a beginner alok 4 3,216 Jul-30-2021, 11:37 AM
Last Post: metulburr
  Nested Python functions (Dan Bader's book) Drone4four 4 2,751 Jun-26-2021, 07:54 AM
Last Post: ndc85430
  I really need help, I am new to python, I am using a book that helps me to learn JaprO 5 3,262 Nov-28-2020, 02:30 PM
Last Post: JaprO
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,473 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  listdir on IP Adress OEMS1 3 3,088 Jul-19-2020, 06:01 PM
Last Post: bowlofred
  Creating Nested Dictionaries Confusion gw1500se 2 2,259 May-18-2020, 11:16 PM
Last Post: gw1500se
  creating a list of dictionaries from API calls AndrewEnglsh101 5 3,295 Apr-03-2020, 02:21 PM
Last Post: AndrewEnglsh101
  Data Dictionaries in Python mrsenorchuck 16 7,783 Nov-25-2019, 09:29 PM
Last Post: mrsenorchuck

Forum Jump:

User Panel Messages

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