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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:20:06+00:00 2026-06-14T08:20:06+00:00

I want to write an iterator for my ‘toy’ Trie implementation. Adding already works

  • 0

I want to write an iterator for my ‘toy’ Trie implementation.

Adding already works like this:

class Trie:
    def __init__(self):
        self.root = dict()
        pass
    def add(self, string, value):
        global nops
        current_dict = self.root
        for letter in string:
           nops += 1
           current_dict = current_dict.setdefault(letter, {})
        current_dict = current_dict.setdefault('value', value)              
        pass

The output of the adding looks like that:

trie = Trie()
trie.add("hello",1)
trie.add("world",2)
trie.add("worlds",12)
print trie.root
{'h': {'e': {'l': {'l': {'o': {'value': 1}}}}}, 'w': {'o': {'r': {'l': {'d': {'s': {'value': 12}, 'value': 2}}}}}}

I know, that I need a __iter__ and next method.

def __iter__(self):
    self.root.__iter__()
    pass

 def next(self):
    print self.root.next()

But AttributeError: 'dict' object has no attribute 'next'. How should I do it?

[Update] In the perfect world I would like the output to be one dict with all the words/entries with their corresponding values.

  • 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-14T08:20:07+00:00Added an answer on June 14, 2026 at 8:20 am

    Your __iter__ special method should return an iterator; that is, an object of a class that you can call next on. A toy iterator class would be something like:

    class MyIterator:
        def __init__(self):
            self.i = 10
        def next(self):
            self.i -= 1
            if self.i == 0:
                raise StopIteration
            else:
                return self.i
    

    Unless you have a natural object to call iter on, it’s usually easier to make __iter__ a generator:

    def __iter__(self):
        for i in range(10)
            yield i
    

    Here’s a stack-based generator iterator for your Trie:

        def __iter__(self):
            stack = [('', self.root)]
            while stack:
                prefix, d = stack.pop()
                for k, v in d.items():
                    if k == 'value':
                        yield prefix, v
                    else:
                        stack.append((prefix + k, v))
    

    You could also try writing it recursively, although you’d need to use itertools.chain or yield from (only since Python 3.3).

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

Sidebar

Related Questions

I'm trying to write my first iterator class / container type. Basically, I want
I want to write a class with three int values in them and manipulate
I want to filter xml based on query string my xml is like this
Possible Duplicate: Obtaining const_iterator from iterator I want to write a metafunction which returns
I am going to write an adapter class. In this class there is an
So I am using directory iterator to get all php files like this: $file_list
I like to write my class declarations in a header file and defining it
i want to pick phone number from contact list i write this code to
I want write a simple query which will fetch data from a table (which
I want write a little code analyzer which parses nested structures and translates into

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.