I am working on a bot that would work with ical files and the events inside them. The way it works is that the ical file is read in to a variable data that is then parsed by the ics library and converted in to a list of events. The variable eventlist is a <class 'list'> containing elements of the type: <class 'ics.event.Event'>
from ics import Calendar
import time
from datetime import datetime
#open the ical file for processing
with open("D:\\Downloads\\d210107c532744427a.ics", 'r') as file:
data = file.read()
c = Calendar(data)
eventlist = list(c.events) #create a list of all the events
However, when I try to iterate over eventlist using a for loop the iterator is not an integer rather it becomes a <class 'ics.event.Event'> example below:
for x in eventlist:
print(type(x))
Output of the above code is:
<class 'ics.event.Event'>
<class 'ics.event.Event'>
<class 'ics.event.Event'>
...
This is a problem because when trying to access an element from eventlist using eventlist[x] an error occurs since x need to be an integer.
Is there any way to suppress this behavior and make the for loop return integers for the iterator?
Edit 1: I understood that this is how for loops are supposed to behave in Python, however I would also like to know if there is a way to get the position if the current x in the list as an integer?
xis already "an element fromeventlist". Just usexinstead ofeventlist[x].forloops always iterate over the elements of an iterable. There is no way to "suppress" that behavior, and there is no good reason to want to. The are various built-in functions, e.g.enumeratethat provide various iterators that you may find useful.__iter__method, the iterator implements a__next__method (along with an__iter__method itself that simply returnsself)for index, event in enumerate(eventlist): ...