Python Forum
Can I replace IF statements with a key lookup ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I replace IF statements with a key lookup ?
#1
If I have code like the following ( from https://python-forum.io/Thread-Convert-e...VCF-format ):

    name = ""
    if 'cn' in record:
        name = record['cn'][0]
    
    surname = ""
    if 'sn' in record:
        surname = record['sn'][0]
    
    given_name = ""
    if 'givenName' in record:
        given_name = record['givenName'][0]
    
    display_name = ""
    if 'displayName' in record:
        display_name = record['displayName'][0]
    
    email = ""
    if 'mail' in record:
        email = record['mail'][0]
and the input data is from an LDIF formatted file, and the record is coming from a dictionary/class as such

Quote:OrderedDict([('objectClass', ['inetOrgPerson']), ('cn', ['Forrest Gump']), ('sn', ['Gump']), ('displayName', ['Forrest Gump']), ('mail', ['[email protected]'])])

can I use some sort of key lookup as explained at Python: How Key lookup works in Dictionary and how to customize it? ??

simply to replace all the ugly IF statements with a simple key lookup ??
Reply


Messages In This Thread
Can I replace IF statements with a key lookup ? - by jehoshua - Mar-05-2021, 09:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  2-dataframe, datetime lookup problem Mark17 0 1,749 Jan-27-2022, 01:02 AM
Last Post: Mark17
  Python VLookup? Lookup Table? Nu2Python 3 3,792 Oct-25-2021, 08:47 PM
Last Post: Nu2Python
  python 3 dns lookup private domain didact 1 4,090 Sep-19-2020, 06:01 PM
Last Post: bowlofred
  Partial key lookup in dictionary GaryNR 1 4,545 Jul-16-2020, 06:55 PM
Last Post: Gribouillis
  Encoding and mac-vendor-lookup library tuanjggaa 1 3,462 Mar-27-2020, 03:12 PM
Last Post: deanhystad
  Excel Lookup riteshprakash 0 2,322 Sep-11-2019, 12:43 PM
Last Post: riteshprakash
  fast lookup for array markB 3 5,085 May-13-2019, 12:11 AM
Last Post: scidam
  lookup tables Skaperen 4 4,273 Aug-13-2018, 06:43 AM
Last Post: Gribouillis
  Search & Replace - Newlines Added After Replace dj99 3 4,282 Jul-22-2018, 01:42 PM
Last Post: buran
  Lookup tables parrytoss 0 3,119 Feb-07-2018, 08:45 AM
Last Post: parrytoss

Forum Jump:

User Panel Messages

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