Python enumerate()

So I’ve found that enumerate does weird things to the index when you don’t enumerate from the beginning. How do I force the index to be correct?

Example:

mylist1=range(10,20)
mylist2=range(20,30)
list1=[]
list2=[]
#I want to skip the first item
for i,j in enumerate(mylist[1:]):
    list1.append(i)
    list2.append(j+mylist2[i])

You would expect that list1 contained 1,2,3,4,5,6,7,8,9, but it actually contains 0,1,2,3,4,5,6,7,8. I need the enumerated index to match the index of j in the list or when I try to get mylist2[i], my values will be off my one index. I know that enumerate is working correctly by doing this, but how can I get it to do it the way that I have described?

Okay, so I found out that you can determine what number enumerate starts by doing enumerate(mylist1[1:],1). But that does not solve the problem if I do enumerate(mylist1[::2]).

@lawrenceyy, every item in a list is an object from which you can get the index eg:

mylist = range(10, 20)
print mylist.index(14)

should print 4. Does that help ?

c.

But having to extract the index kind of defeats the purpose of enumerate. I guess, I’ll just have to create as list of desired indices and iterate over them.

Does index() work from Rhino geometry also? I know that it works for strings and numbers, but I’m never sure if these kinds of methods work for abstract things like a surface.

Hm, if you know you´re just going to skip the first item couldn’t you just add 1 to the variable i ?

#I want to skip the first item
skip = 1
for i,j in enumerate(mylist[skip:]):
    list1.append(i+skip)
    list2.append(j+mylist2[i+skip])

It depends on the geometry type. If it is an item in an iterable, you can just access it by using an index eg.

brep_face = brep.Faces[10]

and to get the faces’s index later use:

print brep_face.FaceIndex

For lists, the index can be used as shown above:

import rhinoscriptsyntax as rs

def DoSomething():

    ids = rs.GetObjects("Select", 0, False, True, False)
    if not ids: return
    
    for id in ids:
        print ids.index(id), id

DoSomething()

c.

@lawrenceyy, i agree, enumerate is just zero based or based on the start index you provide. I´ve thought about it and found that to avoid using

mylist.index(14)

you might as well do this:

mylist1 = xrange(10,20)
mylist2 = xrange(len(mylist1))

for index, item in zip(mylist2, mylist1)[1::2]:
    print index, item

It is not only faster but allows to apply any slicing to mylist1 and still returns the indices of the unsliced version of mylist1, eg. skipping the first item and getting every second as shown above.

c.

1 Like

Thanks, @clement

This zip method is interesting. I find myself doing a lot of operations where I need to use items from multiple lists, but often at the same index. This method can potentially relieve me from having to do for i in range(len(mylist1)): . I can just tap into all lists at once like this:

for name,age,gender in zip(listname,listage,listgender):
    print name
    print age
    print gender