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 8278247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:56:46+00:00 2026-06-08T08:56:46+00:00

Essentially I have a list of items each with a different type such as

  • 0

Essentially I have a list of items each with a different type such as

['a',1,'b',2,3,'c'] 

or

[{"A":1},1,{"B":2},{"C":3},"a"]

and I would like to split these into two seperate lists, retaining the original order

[[ 'a', None,  'b', None, None,  'c'],
 [None,    1, None,    2,    3, None]]

or

[[{"A":1}, None, {"B":2},{"C":3}, None],
 [None,       1,    None,   None, None],
 [None,    None,    None,   None,  "a"]]

What I have :

def TypeSplit(sources)
  Types = [dict(),str(),num()]
  return [[item for item in sources if type(item) == type(itype)] for itype in types]  

Though this doesn’t fill in None.

The reason I’m doing this is that I will be given a list with different types of info and need to flesh it out with other values that compliment the original list.

Is there a better way to do this ?

  • 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-08T08:56:47+00:00Added an answer on June 8, 2026 at 8:56 am

    This is a good use-case for the conditional expression. Also, I’m assuming you’d like to do this in as generalized a way as possible, so instead of using a fixed list of types, I’d suggest generating the list dynamically:

    def type_split(sources):
        types = sorted(set(type(i) for i in sources))
        return [[item if type(item) == itype else None for item in sources] 
                for itype in types]  
    

    If you need to use a fixed list (and you know that your input list won’t contain anything but those types and their subclasses), you could do this:

    import collections
    import numbers
    def type_split(sources):
        types = [basestring, collections.Mapping, numbers.Number]
        return [[item if isinstance(item, itype) else None for item in sources] 
                for itype in types] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that essentially a list of XPaths like so: /Options/File[1]/Settings[1]/Type[1] /Options/File[1]/Settings[1]/Path[1]
Essentially I have a list of items, I want to have some type of
Essentially I would like to have a messages.properties files external to the jar files
I have a single page backbone application. It's essentially a list of items with
I have two essentially separate applications for configuring two pieces of hardware sold by
I have a list of objects, each with a bool ShouldRun() method on them.
Suppose I have a List<T> with 1000 items in it. I'm then passing this
I've run into a problem where I want to display a list of items
Essentially, I have a very large List containing in turn relatively large Dictionaries. So
I am currently trying to manipulate a unordered list with jQuery, essentially I have

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.