-4

Say I have a

list=['apple','orange','durian','blackberry']

How do I find the position of 'durian' using while or for loops I know there's a code called list.index('durian') but I want to know the position of the specific item using for/while loops

Jan
  • 42,290
  • 8
  • 54
  • 79

5 Answers5

1

You could use enumerate(...):

lst = ['apple', 'orange', 'durian', 'blackberry']

for idx, value in enumerate(lst):
    if value == "durian":
        print(idx)
Jan
  • 42,290
  • 8
  • 54
  • 79
0

One way:

while list.pop() != 'durian':
    pass
print(len(list))

Another:

i = 0
while list[i] != 'durian':
    i = random.randrange(len(list))
print(i)

Imho more fun than boring range/enumerate :-)

superb rain
  • 5,300
  • 2
  • 11
  • 25
  • Wouldn't you first suggesting produces a run time error if the item want in the list? This seems like a poor UI. Better (though not best) to silently print nothing if it wasn't found. I hope to never run across code like your second suggestion. – SherylHohman Jul 29 '20 at 00:53
  • @SherylHohman Yes, just like the `list.index('durian')` mentioned by the OP would. And like I mentioned, these are for fun. I think it's obvious I'm not seriously suggesting to use these in something serious. – superb rain Jul 29 '20 at 01:12
  • @SherylHohman Oh and the second one can actually be a legitimately good solution. Depends on the problem. You can find that exact algorithm [here](https://en.wikipedia.org/wiki/Randomized_algorithm#Motivation), for solving the problem of finding an instance of a value that appears half the time. And as mentioned there, it's good against attackers (e.g., someone putting all occurrences in the second half of the list, so that all those range/enumerate solutions and even list.index take O(n), while the randomized one is about O(1)). – superb rain Jul 29 '20 at 09:20
0

You can use a for loop iterating over the length of the list using range(len(list)) as shown below. len(list) returns the number of indexes in the list , so that will be 3 in this case, and using the range() function will help iterate/loop over the list that many times. Remember, in a list, the first value is at the 0th index, second value is at the 1st index and so on.

list_sample = ['apple', 'orange', 'durian', 'blackberry']

for i in range(len(list_sample)):
    if list_sample[i] == 'durian':
        print("Index Position of 'durian' in the list is " + str(i))
    else:
        pass

or You can use the enumerate() function as below:

list_sample = ['apple', 'orange', 'durian', 'blackberry']  # apple is at the

for i, value in enumerate(list_sample):
    if value == 'durian':
        print("Index Position of 'durian' in the list is " + str(i))
    else:
        pass

Varun
  • 1
  • 2
-1

Why you want to that?

If you really want, the code function is easy

def getindex(l: list, element) -> int:
    for i, e in enumerate(l):
        if e == element:
            return i
    raise ValueError(f"{element} is not in list")
-2

In python, list[int] is used to find the position an element in a list, where int is the index of the item you want.

In your case, this would be list[2]. Note that we start counting at index 0.

Run_Script
  • 2,487
  • 2
  • 15
  • 30
Kennoh
  • 137
  • 7