0

How can we get the previous and next item of a list in python? My code isn't working as it was supposed to. It always returns me 0th index of list. I dont want to use loop in this method.

Here's my code:

def provide_activetab_index(self,index):
        if index > 0:
            return index -1
        if index < len(self.activetabs) - 1:
            return index + 1

When i call this method it returns 0 when the self.current is 0.

    def next(self):
        index = self.provide_activetab_index(self.current+1)
        self.display(self.frames[index])

    def previous(self):
        index = self.provide_activetab_index(self.current-1)
        self.display(self.frames[index])

3 Answers 3

1
def next(self):
    index = self.provide_activetab_index(self.current,1)
    self.display(self.frames[index])

def previous(self):
    index = self.provide_activetab_index(self.current,-1)
    self.display(self.frames[index])

def provide_activetab_index(self,index,new):
    if (index >= 0 and new == 1 and index < len(self.activetabs)-1) or (new == -1 and index > 0 and index < len(self.activetabs)):
        return index + new
    return index

EDIT

def provide_activetab_index(self,index,new):
    l = len(self.activetabs)-1
    if index >= l and new == 1:
        return 0 # Return fist element if no last element
    if index <= 0 and new == -1:
        return l # Return last element if no previous element
    else:
        return index + new

To update self.current everytime

def next(self):
    self.current = self.provide_activetab_index(self.current,1)
    self.display(self.frames[self.current])
def previous(self):
        self.current = self.provide_activetab_index(self.current,-1)
        self.display(self.frames[self.current])
Sign up to request clarification or add additional context in comments.

10 Comments

It stucked after the last value. Can't reach to the 0th index after the last index of list.
I have edited the answer, What you wish to do after last index
TypeError: provide_activetab_index() takes 2 positional arguments but 3 were given
Please check index = self.provide_activetab_index(self.current,1), I have passed 2 arguments
I hope my future answers from you sir. thank you again.
|
1

Hi i couldn't understand what you want exactly because there are few inputs that i don't know like self.current or self.activetabs so it is hard to modify your algorithm but there is data structure called "doubly_linked_list" you can implement that easily and than go to next or prev item with itemname.next or itemname.prev.Here is code

class Node:

    def __init__(self, data, next_n=None, prev_n=None):
        self.data = data
        self.next = next_n
        self.prev = prev_n

class Doubly_linked_list:
        def __init__(self):
           self.size = 0
           self.root = None  # points last member added
           self.last = None  # points first member added first member added is accepted as last member end next is None

       def add(self, data):
            if self.size == 0:
                new_node = Node(data)
                self.root = new_node
                self.last = new_node

            else:
                new_node = Node(data)
                new_node.next = self.root
                self.root.prev = new_node
                self.root = new_node
            self.size += 1

        def remove(self, data):
            this_node = self.root
            while this_node is not None:
                if this_node.data == data:
                    if this_node.next is not None:
                        if this_node.prev is not None:
                            this_node.prev.next = this_node.next
                            this_node.next.prev = this_node.prev
                        else:
                            this_node.next.prev = None
                            self.root = this_node.next
                    else:
                        this_node.prev.next = None
                        self.last = this_node.prev
                    self.size -= 1
                    return True
                else:
                    this_node = this_node.next
            return False

        def find_all(self, data):
            this_node = self.root
            while this_node is not None:
                if this_node.data == data:
                    return data
                elif this_node.next is None:
                    return False
                else:
                    this_node = this_node.next

        def print_all(self):
            if self.root is None:
                return None
            this_node = self.root
            print(this_node, end="-->")
            while this_node.next != self.root:
                this_node = this_node.next
                print(this_node, end="-->")
            print()

if you insist on finding nodes with indexes you can create a list object inside doubly_linked_list class inside init function self.list = [] and add all the nodes also to self.list and reach them by indexes.

1 Comment

That's so amazing you did a great job.
0

With your code

def provide_activetab_index(self,index):
    if index > 0:
        return index -1
    if index < len(self.activetabs) - 1:
        return index + 1

when you pass index greater that 0 it will always return index-1.
As you mentioned self.current is 0 and you call it with

def next(self):
    index = self.provide_activetab_index(self.current+1)
    self.display(self.frames[index])

you actually calling

index = self.provide_activetab_index(1)

so this returns you 1-1 = 0

5 Comments

I tried to explain while it is not working. There is no fixed code in my answer
Oh, i need solution. I can't figure out sorry. :(
Hi kanwarprogrammer, SO is not a place where others will just give you working code but rather a place where you get help to solve the problem yourself.
since you dont change the value of self.current it will always be 0 and when you run your next function then it will always return 0. you need to increment the value of self.current
en.wikipedia.org/wiki/…. your explanation is appreciated but this place can also be used to get solutions of problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.