0

I thought this would be straightforward but after a lot of searching I cannot find the answer. I want to return the next item in a list.

So, in the example below I want to return l4.pkl.

list_numbers = ['l1.pkl', 'l2.pkl', 'l3.pkl', 'l4.pkl', 'l5.pkl', 'l6.pkl']
element = 'l3.pkl'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Andy
  • 509
  • 2
  • 7
  • 3
    Like `list_numbers[list_numbers.index(element) + 1]`? Does not handle the "last element" case, though, and if you plan to do this more often you should create a `dict` mapping elements to their indices. – tobias_k Nov 16 '21 at 09:12
  • 3
    are items in the list assured to be unique? what is the expected result if an item appeared in the list more than once? if you are sure there will be no duplicated then you can use the index method of the list to find the position of that item then add 1 – Chris Doyle Nov 16 '21 at 09:13
  • Does this answer your question? [Finding the index of an item in a list](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list) – Be Chiller Too Nov 16 '21 at 09:13
  • What do you want to happen, if you request the element after 'l6.pkl'? – Jblasco Nov 16 '21 at 09:23

5 Answers5

2

Use index

(For the last index which will raise IndexError you should make the adjustments):

ist_numbers = [
    "l1.pkl",
    "l2.pkl",
    "l3.pkl",
    "l4.pkl",
    "l5.pkl",
    "l6.pkl",
    "l7.pkl",
    "l8.pkl",
    "l9.pkl",
    "l10.pkl",
]
print(ist_numbers[ist_numbers.index("l5.pkl") + 1])

Output:

l6.pkl
David Meu
  • 1,527
  • 9
  • 14
  • you could replace it with `min(len(ist_numbers)-1,ist_numbers.index("l5.pkl")+1)` to handle the last element e.g `ist_numbers[min(len(ist_numbers)-1,ist_numbers.index("l10.pkl")+1)]` becomes `l10.pkl` – CutePoison Nov 16 '21 at 09:18
  • 1
    @CutePoison this is bad, as it won't return a next element for the last item so it is 'hiding' the expected behaviour better raising something like 'last element no next element'... Anyway I can do the adjustments but preferred to leave it for the OP – David Meu Nov 16 '21 at 09:24
  • 1
    Agree with that - I've provided an answer with a try/catch based on your answer – CutePoison Nov 16 '21 at 09:26
2

Here's a fun one, using an iterator:

i = iter(list_numbers)
if element in i:  # lazily moves the iterator forward just far enough
    result = next(i, None)  # None is the default if there are no more elements

Or short with a default value:

result = next(i, None) if element in i else None

Some docs:

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • 2
    Somehow it feels like this answer is, at the same time, the most beautiful and the most horrifying answer to the question – Drecker Nov 16 '21 at 09:37
2

Easy way Find Index of '13.pkl' with using :

listName.index('base_element')

now for accessing next element you need to choose next index so you should try this :

target_element = listName[listName.index('base_element')+1]
bembas
  • 762
  • 8
  • 20
0

Many ways to solve this. Simple Logic is

  1. Find Index
  2. return arr[Index+1]

You can use any method for finding index.

index = list_numbers.index(element)
return list_numbers[index+1]
Jblasco
  • 3,827
  • 22
  • 25
Ayush
  • 31
  • 5
  • Furthermore, if your list is sorted, you can use `binom` package to find the index of the elemenet, to make it run faster (does not matter if you have a list of just ~ten items) – Drecker Nov 16 '21 at 09:29
0

Just adding to @David Meus answer

You can do

ist_numbers[min(len(ist_numbers)-1,ist_numbers.index(<element>)+1)]

such that you the last element returns the last element.

Otherwise you can use a try/catch with his answer (wrapped in a function)


def get_next_element(ele,l):
    """
    Returns the next element in the list "l" after the provided element "ele"
    """
    try:
        print(l[l.index(ele) + 1])
    except IndexError:
        raise ValueError(f"'{ele}' is the last element in the list")


get_next_element("l1.pkl",ist_numbers) #"l2.pkl"
get_next_element("l10.pkl",ist_numbers) #ValueError: 'l10.pkl' is the last element in the list
CutePoison
  • 4,679
  • 5
  • 28
  • 63