Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, March 1, 2017

How to get item's position in a list?

How to get item's position in a list?


I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?

Example:

testlist = [1,2,3,5,3,1,2,1,6]  for item in testlist:      if item == 1:          print position  

Answer by jakber for How to get item's position in a list?


for i in xrange(len(testlist)):    if testlist[i] == 1:      print i  

xrange instead of range as requested (see comments).

Answer by zdan for How to get item's position in a list?


Use enumerate:

testlist = [1,2,3,5,3,1,2,1,6]  for position, item in enumerate(testlist):      if item == 1:          print position  

Answer by Chris Cameron for How to get item's position in a list?


If your list got large enough and you only expected to find the value in a sparse number of indices, consider that this code could execute much faster because you don't have to iterate every value in the list.

lookingFor = 1  i = 0  index = 0  try:    while i < len(testlist):      index = testlist.index(lookingFor,i)      i = index + 1      print index  except ValueError: #testlist.index() cannot find lookingFor    pass  

If you expect to find the value a lot you should probably just append "index" to a list and print the list at the end to save time per iteration.

Answer by Charlie Martin for How to get item's position in a list?


Hmmm. There was an answer with a list comprehension here, but it's disappeared.

Here:

 [i for i,x in enumerate(testlist) if x == 1]  

Example:

>>> testlist  [1, 2, 3, 5, 3, 1, 2, 1, 6]  >>> [i for i,x in enumerate(testlist) if x == 1]  [0, 5, 7]  

Update:

Okay, you want a generator expression, we'll have a generator expression. Here's the list comprehension again, in a for loop:

>>> for i in [i for i,x in enumerate(testlist) if x == 1]:  ...     print i  ...   0  5  7  

Now we'll construct a generator...

>>> (i for i,x in enumerate(testlist) if x == 1)    >>> for i in (i for i,x in enumerate(testlist) if x == 1):  ...     print i  ...   0  5  7  

and niftily enough, we can assign that to a variable, and use it from there...

>>> gen = (i for i,x in enumerate(testlist) if x == 1)  >>> for i in gen: print i  ...   0  5  7  

And to think I used to write FORTRAN.

Answer by Leonardo for How to get item's position in a list?


testlist = [1,2,3,5,3,1,2,1,6]  for id, value in enumerate(testlist):      if id == 1:          print testlist[id]  

I guess that it's exacly what you want. ;-) 'id' will be always the index of the values on the list.

Answer by Phil Rankin for How to get item's position in a list?


Here is another way to do this:

try:     id = testlist.index('1')     print testlist[id]  except:     print "Not Found"  

Answer by mmj for How to get item's position in a list?


What about the following?

print testlist.index(element)  

If you are not sure whether the element to look for is actually in the list, you can add a preliminary check, like

if element in testlist:      print testlist.index(element)  

or

print(testlist.index(element) if element in testlist else None)  

or the "pythonic way", which I don't like so much because code is less clear, but sometimes is more efficient,

try:      print testlist.index(element)  except ValueError:      pass  

Answer by user1713952 for How to get item's position in a list?


[x for x in range(len(testlist)) if testlist[x]==1]  

Answer by xXAngelJinXx for How to get item's position in a list?


I think that it might be useful to use the curselection() method from thte Tkinter library:

from Tkinter import *   listbox.curselection()  

This method works on Tkinter listbox widgets, so you'll need to construct one of them instead of a list.

This will return a position like this:

('0',) (although later versions of Tkinter may return a list of ints instead)

Which is for the first position and the number will change according to the item position.

For more information, see this page: http://effbot.org/tkinterbook/listbox.htm

Greetings.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.