Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:13:16+00:00 2026-05-30T04:13:16+00:00

Here is my issue: I am doing an LDAP search in Python. The search

  • 0

Here is my issue:

I am doing an LDAP search in Python. The search will return a dictionary object:

{'mail':['user@mail.com'],'mobile':['07852242242'], 'telephoneNumber':['01112512152']}

As you can see, the returned dictionary contains list values.

Sometimes, no result will be found:

{'mail':None, 'mobile':None, 'telephoneNumber':['01112512152']}

To extract the required values, I am using get() as to avoid exceptions if the dictionary item does not exist.

return {"uname":x.get('mail')[0], "telephone":x.get('telephoneNumber')[0], "mobile":x.get('mobile')[0]}

I want to return my own dictionary as above, with just the string values, but Im struggling to find an efficient way to check that the lists are not None and keep running into index errors or type errors:

(<type 'exceptions.TypeError'>, TypeError("'NoneType' object is unsubscriptable",)

Is there a way to use a get() method on a list, so that if the list is None it wont throw an exception???

{"uname":x.get('mail').get(0)}

What is the most efficient way of getting the first value of a list or returning None without using:

if isinstance(x.get('mail'),list):

or

if x.get('mail') is not None:
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:13:18+00:00Added an answer on May 30, 2026 at 4:13 am

    If you want to flatten your dictionary, you can just do:

    >>> d = {'mail':None, 'mobile':None, 'telephoneNumber':['01112512152']}
    >>> 
    >>> dict((k,v and v[0] or v) for k,v in d.items())
    {'mail': None, 'mobile': None, 'telephoneNumber': '01112512152'}
    

    If you’d also like to filter, cutting off the None values, then you could do:

    >>> dict((k,v[0]) for k,v in d.items() if v)
    {'telephoneNumber': '01112512152'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys, i have a little issue here. Im doing a search to my UITableView.
So I've recently started doing Object Oriented Programming with Python, and for a specific
My issue here is quite simple : i'm trying to use the jQuery validate
Having an issue here that I have tried everything I can think of but
I have a rather annoying issue here I can't get my CheckBox CheckedChange event
I'm having a small issue here: <div id=navbar> <ul id=navtabs class=floatcontainer> <li <?php if
I have a curious issue here In my ejbCreate() method from where i insert
I'm having an issue here, I am trying to get all the titles from
I've a small issue here. I am using an if statement with UIAlertView and
Hey guys I have a little issue here. I have a panel where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.