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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:35:52+00:00 2026-05-23T07:35:52+00:00

I like the Python syntax a lot, but as I’m coming from C++ I

  • 0

I like the Python syntax a lot, but as I’m coming from C++ I don’t get one thing about iterators in Python. In C++ there are 2 kinds of iterators – constant and modifying (non-const). In python it seems (for what I’ve seen) like there is only the first kind and if you want to modify the elements, you have to use the indexing, which doesn’t feel comfortable and so generic to me.
Let me illustrate with a simple example:

ab = ["a", "b"]
for (index, lett) in enumerate(ab):
    print "Firstly, lett is ab[index]?", lett is ab[index]
    lett = str(index)
    print lett
    print ab[index]
    print "But after, lett is ab[index]?", lett is ab[index]

So I wasn’t able to modify the list with the iterator.
It just makes Lazy copies (see Wikipedia) as I discovered with the is operator, so is there a way to say I want it to be a directly modifying iterator instead using the neat

for variable in iterable_object:

syntax?

  • 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-23T07:35:52+00:00Added an answer on May 23, 2026 at 7:35 am
    def iset(iterable):
        for i, x in enumerate(iterable):
            def setter(value):
                iterable[i] = value
            yield setter, x
    a = range(10)
    for set_x, x in iset(a):
        set_x(x * 2)
    print a
    

    prints

    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there something like python's interactive REPL mode, but for Java? So that I
I occasionally see the list slice syntax used in Python code like this: newList
When I saw this article about using a python-like sintax (indentation based, without curly
Is there something like Python's getattr() in C#? I would like to create a
Is there a way to have multiline strings in VB.NET like Python a =
I like using Python, because of the easy-to-learn syntax, however, I recently learned it
I've started using bpython for my python interpreting needs. I like the syntax highlighting
I have few experience with languages like Python, Perl and Ruby, but I have
An idiom commonly used in OO languages like Python and Ruby is instantiating an
I'm not sure if I like Python's dynamic-ness. It often results in me forgetting

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.