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

  • Home
  • SEARCH
  • 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 9204595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:44:49+00:00 2026-06-17T23:44:49+00:00

Is there a standard library function or more pythonic way to do this ?

  • 0

Is there a standard library function or more pythonic way to do this ?

def itemize(i):
    if type(i) is list:
        return i
    return [i]

Context:

Useful in db (nosql style) migrations from single value to a list

doc = <get doc from db>
for i in itemize(doc.var1):
    #blah

or

doc.var1 = itemize(doc.var1)
  • 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-06-17T23:44:51+00:00Added an answer on June 17, 2026 at 11:44 pm

    Usually, enforcing this sort of thing is a bad idea. In many cases, the most pythonic thing you can do is check if your item is a sequence:

    import collections
    def to_sequence(item):
        return item if isinstance(item,collections.Sequence) else [item]
    

    One instance where this passes something that you may not want to pass is with strings … strings are sequences, so to_sequence('foo') will return 'foo', not ['foo'] — but it’s unlikely that you want it to be ['f','o','o'] either … so you might need to special case for that if it’s desired.

    One simple fix for that would be (for python2.x):

    import collections
    def to_sequence(item):
        return item if isinstance(item,collections.Sequence) and hasattr(item,'__iter__') else [item]
    

    But again, usually the “pythonic” thing to do is to defer this sort of checking and try to use the object in some context — if it fails, then you can do something to react.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a standard way / C# library to convert a string into a
This is a weird question, but is there a standard way to manipulate the
I am wondering if there is a standard library function in Python which will
Is there a standard library function which will set a minimum value to a
Possible Duplicate: CSV File Imports in .Net In .net, is there a standard library
Is there a standard Java library that handles common file operations such as moving/copying
Is there a Fortran standard library (I keep hearing about something called ISO_C_BINDING, but
Is there a C++ Standard Template Library class that provides efficient string concatenation functionality,
Is there a standard mechanism or known library that will convert .png images to
Is there any standard way of debugging Javascript on a webpage that's being accessed

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.