Benn
Benn

Reputation: 189

Add two list to one list

I'm trying to marge two lists that I created through a class to a new list. I use the __add__ function. But it constantly adds the first two indexes and stops.

This is my code:

class Stack:
    def __init__(self):
        self.__items = []
        self.__top = 0

    def is_Empty(self):
        return self.__top <= 0
        try:
            raise Exception('Stack empty.')
        except Exception as error:
            print(error)

    def __str__(self):
        """Print current stack"""
        if self.is_Empty() == True:
            return "Stack empty"
        else:
            return "Stack is not empty"

    def push(self, item):
        """Push item in stack."""
        self.__items.append(item)
        self.__top += 1

    def pop(self):
        """Remove top of the stack."""
        if self.__top <= 0:
            return self.is_Empty()
        self.__top -= 1
        return self.__items.pop()

    def top(self):
        """Return top of the stack."""
        if self.__top <= 0:
            return self.is_Empty()
        else:
            return self.__items[-1]

    def my_stack(self):
        """Show the current stack"""
        if self.__items == []:
            return self.is_Empty()
        else:
            return f"The current stack is {self.__items}"

    def __add__(self,other):
        """Add two lists together"""
        newlst = []
        for i, j in zip(self.__items, other.__items):
            newlst.append(i+j)
            return newlst

    def __eq__(self, other):
        """Return True if two list is equal else Return False """
        return (self is other) or (self.__items) == (other.__items)

for example:

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack2 = Stack()
stack2.push(4)
stack2.push(5)
stack2.push(6)

Now I'm trying to add the two stacks into stack3:

stack3 = stack + stack2

The result I get is this:

>>> print(stack3)
[5]

My goal is to get stack3 like this:

>>> print(stack3)
[1, 2, 3, 4, 5, 6]

Upvotes: 0

Views: 66

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51643

Your current code

   def __add__(self,other):
        """Add two lists together"""
        newlst = []
        for i, j in zip(self.__items, other.__items):
            newlst.append(i+j)
            return newlst

adds the first elements of each .__items list and returns the result after that.

To add the inner lists one after each other and return as list, this would work:

def __add__(self, other):
    """Add two lists together"""
    if isinstance(other,Stack):
        return self.__items + other.__items

    return [] # or self.__items or raise Exception

To return a new Stack-instance you could do:

def __add__(self, other):
    """Add two lists together"""
    if isinstance(other, Stack):
        s = Stack()
        for item in self.__items:
            s.push(item)
        for item in other.__items:
            s.push(item)
         return s
    return [] # or self.__items or raise Exception

Upvotes: 1

Related Questions