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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:53:36+00:00 2026-06-15T00:53:36+00:00

How do i take multiple arbitrary values from different index positions in an iterator?

  • 0

How do i take multiple arbitrary values from different index positions in an iterator?

How to get the n next values of a generator in a list (python) and Get the nth item of a generator in Python describe the use of itertools.islice for taking arbitrary element or continuous subset from an iterator. But what if i want multiple arbitrary elements from different positions in the iterator, where you can’t just use islice‘s step argument?

I’m trying to solve Project Euler’s problem 40. I generated a string of concatenated integers with

iteration = (i for i in ''.join(map(str, (i for i in xrange(1,10**6)))))

An now i want to get elements with indexes 1, 10, 100, 1000, 10000, 100000, 1000000 counting from 1. I couldn’t use islice here, because every call to next shifts current value to yield to the right. For example

next(islice(iteration, 1, 2)) + next(islice(iteration, 3, 4))

produces ’26’ instead of ’24’.

Update (25.11.12, 4:43 UTC+0):

Thanks for all the suggestions. My current code looks like:

it = (i for i in ''.join(map(str, (i for i in xrange(1,10**6)))))
ds = [int(nth(it, 10**i-10**(i-1)-1)) for i in range(7)]
return product(ds)

The ugly argument for nth is to generate a sequence of 0, 8, 89, 899, 8999 etc.

  • 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-15T00:53:38+00:00Added an answer on June 15, 2026 at 12:53 am

    This is from the “recipes” section of the itertools documentation. It returns the nth element of iterable, consuming it as it goes:

    def nth(iterable, n, default=None):
        "Returns the nth item or a default value"
        return next(islice(iterable, n, None), default)
    

    You can get the 1st, 10th, 100th etc elements by calling it sequentially (noting that the iterator gets consumed, and is zero-indexed):

    first = nth(iteration, 0)
    tenth = nth(iteration, 8)  # since we've already taken one
    hundredth = nth(iteration, 89)  # since we've already taken ten
    # etc
    

    Alternatively, you could use tee and use nth with a different iterator each time. This way you don’t have to worry about the fact that your single iterator is getting consumed. On the other hand you might start swallowing memory if your iterators are very long.

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

Sidebar

Related Questions

How do I take a set of polygons which contain arbitrary values and create
I take multiple lines as a input from a JTextarea, If I write it
I am try ing to take multiple pictures with iphone camera from the same
I have a string resource = /Music/1 the string can take multiple numeric values
I want to take multiple integer inputs in same line eg :- input -1
Why was java defined such that methods may take as input multiple parameters, but
I have a table of student scores. Students can take the test multiple times,
There are multiple overloads for sqlcommand.Parameters.Add(...) function. Some of them do not take size,
This question could easily take multiple paths, so I will hit the more specific
Or, practically, how can I sort a list of dictionaries by multiple keys? 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.