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

The Archive Base Latest Questions

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

I’m studying Python and I don’t understand how to use iterators. I need to

  • 0

I’m studying Python and I don’t understand how to use iterators.

I need to write code. In C it’ll be like this:

list_node *cp = list_of_chars;
char dp = '>'; int flag = 0;
while (cp != NULL)
{ if( isdigit(cp->val) ) 
  { printf("%c",cp->val);
    if( cp->val == '0' )
    { cp->prev->next = cp->next; cp->next->prev = cp->prev; }
    else cp->val--;
  }
  else if( (cp->val == '>') || (cp->val == '<') )
  { dp = cp->val; flag = 1; }

       if( dp == '>' ) cp = cp->next;
  else if( dp == '<' ) cp = cp->prev;
  else return ERR;

  if( flag && ( (cp->val == '>') || (cp->val == '<') ))
  { cp->prev->prev->next = cp;
    cp->prev = cp->prev->prev;
  }
}

Can you help me translate this code to python? I started to write, but have some errors and I not sure, that I understand documentation.

  ip = {'cp' : iter(program), 'dp' : '>'}
  flag = 0

  while ip['cp'] != []:

    if ('0' <= ip['cp']) & (ip['cp'] <= '9'):
      print ip['cp']
      if ip['cp'] == '0': ip['cp']  = []
      else:               ip['cp'] -= 1

    elif (ip['cp'] == '>') | (ip['cp'] == '<'):
      ip['dp'] = ip.['cp']
      flag = 1

    else: raise NameError('incorrect cp-value')

    if   ip['dp'] == '>': ip['cp'].next()
    elif ip['dp'] == '<': ip['cp'].prev()
    else: raise NameError('incorrect dp-value')

    if flag & ( (ip['cp'] == '>') | (ip['cp'] == '<') ):
      ip['cp'].prev()
      ip['cp'] = []

The question is how can I get the value of the iterator without the function next().

Examples of python-expert code with advanced uses of iterators would also be nice to see.

  • 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-14T15:55:30+00:00Added an answer on June 14, 2026 at 3:55 pm

    Okay here are lots of problems in you python code.

    Well start with the simple

    while ip['cp'] != []:
    

    Could be written to simply

    while ip['cp']:
    

    It will do while ip['cp'] isn’t a falsly value. [], None, '' are a falsly value for example.

    Instead of:

    if   ip['dp'] == '>': ip['cp'].next()
    elif ip['dp'] == '<': ip['cp'].prev()
    else: raise NameError('incorrect dp-value')
    

    write

    if ip['dp'] == '>': 
        ip['cp'].next()
    elif ip['dp'] == '<':
        ip['cp'].prev()
    else: 
        raise NameError('incorrect dp-value')
    

    Even if in some case, writing inline code can work. Think about readability. Inline code is almost never a good start to make code readable and easy to debug. It will also make 90% of your syntax error easy to fix.

    Here’s where the fun starts…

      if( cp->val == '0' )
      { cp->prev->next = cp->next; cp->next->prev = cp->prev; }
      else cp->val--;
    

    and

      if ip['cp'] == '0': ip['cp']  = []
      else:               ip['cp'] -= 1
    

    In python ‘0’ is the string 0. In python you’re working with string when you have a string object. You’re working with numbers when you have numbers… In other words the code you’re trying to write in python can’t be purely translated to python. The last char in a python string is not ‘\0’. It’s the last char in the string. In other words, you can really test for the last char like in C.

    Now lets talk about iterators

    That said, disregarding all other problems in your code, here is how to work with iterators.

    iter() is a builtin function that returns an iterator. In most case you don’t have to call it yourself. There are constructions that will do that for you.

    for example, you can write :

    iterable = iter('string')
    iterable.next() == 's'
    iterable.next() == 't'
    ... Until StopIteration is raised
    
    for i in iter('string'):
    

    Is the same as:

    for i in 'string':
    

    That said, iterators are used to iter over something. What you are really looking for isn’t an iterator. Because iterator are one way thing. You can go to the end and you can’t go back. There is no previous. Your C code implement a linked-list that can be used with an iterable but in your code, you’re changing the position of some nodes.

    If you want to write python code, don’t write C code in python

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.