Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Here is an example of its usage:

>>> l = LinkedList()
>>> l.insert(1)
>>> l.insert(2)
>>> for x in l:
...     print x
... 
2
1
>>> list(l)
[2, 1]
>>> print(l)
[2, 1]

Note that I used is not None instead of != None, read the answers herehere, if you want to know why.

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Here is an example of its usage:

>>> l = LinkedList()
>>> l.insert(1)
>>> l.insert(2)
>>> for x in l:
...     print x
... 
2
1
>>> list(l)
[2, 1]
>>> print(l)
[2, 1]

Note that I used is not None instead of != None, read the answers here, if you want to know why.

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Here is an example of its usage:

>>> l = LinkedList()
>>> l.insert(1)
>>> l.insert(2)
>>> for x in l:
...     print x
... 
2
1
>>> list(l)
[2, 1]
>>> print(l)
[2, 1]

Note that I used is not None instead of != None, read the answers here, if you want to know why.

added 233 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Here is an example of its usage:

>>> l = LinkedList()
>>> l.insert(1)
>>> l.insert(2)
>>> for x in l:
...     print x
... 
2
1
>>> list(l)
[2, 1]
>>> print(l)
[2, 1]

Note that I used is not None instead of != None, read the answers here, if you want to know why.

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Note that I used is not None instead of != None, read the answers here, if you want to know why.

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Here is an example of its usage:

>>> l = LinkedList()
>>> l.insert(1)
>>> l.insert(2)
>>> for x in l:
...     print x
... 
2
1
>>> list(l)
[2, 1]
>>> print(l)
[2, 1]

Note that I used is not None instead of != None, read the answers here, if you want to know why.

Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

To make @jonrsharpe's suggestion more specific, this is one way to implement __iter__, without having to add another iterator class. It also gives you the __str__ method for free.

def __iter__(self):
    """
    Iterate over the linked list.
    """
    current = self.head
    while current is not None:
        yield current.value
        current = current.next

def __str__(self):
    """
    Prints the current list in the form of a Python list
    """
    return str(list(self))

Note that I used is not None instead of != None, read the answers here, if you want to know why.