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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:03:43+00:00 2026-05-26T06:03:43+00:00

If I have a function that can operate on both sets and lists and

  • 0

If I have a function that can operate on both sets and lists and should return a modified form of the sequence, is there a way to preserve the sequence type but still use a comprehension? For example, in the following if I call stripcommonpathprefix with a set, it works but has the undesired side effect of converting the set to a list. Is it possible to maintain the type (while still using a comprehension) without having to directly check isinstance and then return the correct type based on that? If not, what would be the cleanest way to do this?

def commonpathprefix(seq):

    return os.path.commonprefix(seq).rpartition(os.path.sep)[0] + os.path.sep


def stripcommonpathprefix(seq):

    prefix = commonpathprefix(seq)
    prefixlen = len(prefix)
    return prefix, [ p[prefixlen:] for p in seq ]

Thankyou and sorry if this is a basic question. I’m just starting to learn python.

P.S. I’m using Python 3.2.2

  • 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-26T06:03:43+00:00Added an answer on May 26, 2026 at 6:03 am

    There is no good way to preserve the type of the sequence. As you have guessed, if you really want to do this, you will have to convert the answer at the end to the type you want. It’s quite likely that you don’t need to do this, so you should think hard about it.

    One shortcut that might help you if you do decide to convert: the types of the built-in sequences are also constructors that can create those sequences:

    def strip_common_path_prefix(seq):
        # blah blah
        return prefix, type(seq)(result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Actionscript have a built-in function that accepts a number and can return a
I have a function that can accept any number of arguments... const getSearchFields =
I would like to have a function that can wrap any other function call.
I have a PHP function that requires can take 3 parameteres... I want to
I have a problem with Lucene's scoring function that I can't figure out. So
How can we new primitive types in JNI. I have a function that returns
I have noticed in Java that you can have a function with object... as
I have a javascript function (function1) that checks some global variables (can the user
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
We have a third-party DLL that can operate on a DataTable of source information

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.