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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:55:29+00:00 2026-06-10T10:55:29+00:00

Sometime I write code like this: a,temp,b = s.partition(‘-‘) I just need to pick

  • 0

Sometime I write code like this:

a,temp,b = s.partition('-')

I just need to pick the first and 3rd elements. temp would never be used. Is there a better way to do this?

In other terms, is there a better way to pick distinct elements to make a new list?

For example, I want to make a new list using the elements 0,1,3,7 from the old list. The
code would be like this:

newlist = [oldlist[0],oldlist[1],oldlist[3],oldlist[7]]

It’s pretty ugly, isn’t it?

  • 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-10T10:55:31+00:00Added an answer on June 10, 2026 at 10:55 am

    You can use Python’s extended slicing feature to access a list periodically:

    >>> a = range(10)
    >>> # Pick every other element in a starting from a[1]
    >>> b = a[1::2]
    >>> print b
    >>> [1, 3, 5, 7, 9]
    

    Negative indexing works as you’d expect:

    >>> c = a[-1::-2]
    >>> print c
    >>> [9, 7, 5, 3, 1]
    

    For your case,

    >>> a, b = s.partition('-')[::2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Even experienced programmers write C# code like this sometimes: double x = 2.5; double
I know this is bad practice. Don't write code like this if at all
Sometimes I face I must write a piece of code like this (usually it
I often find I want to write code something like this in C#, but
Sometimes I write code like this solveLogic :: Int -> Int -> Int solveLogic
Sometime in VB.net i have something like: For Each El in Collection Write(El) Next
There's a piece of code that looks like this. The problem is that during
Why does everyone tell me writing code like this is a bad practice? if
I'm trying to write a simple high-low program that will output like this: $
I have something like this in my code typedef struct ts_fem_mesh { double **vertices;

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.