Attendees in Google Calendar not always in the same order
<p>So I've just started using the google calendar api and I've had good results so far. I add attendees with their name and email in the <code>events</code> dictionary, like so</p>

<pre><code>events = { 

              # other stuff here and then this

              'items': [

                   # lots of stuff here, followed by

                  'attendees': [
                     {
                        'email': email1,
                        'displayName': name1
                     },
                     {
                        'email': email2,
                        'displayName': name2
                     },
                  ], 

                  ###

                ]
            }
</code></pre>

<p>Adding them goes fine, but when I access them, I'm never guaranteed of their order. I thought I could just access the emails like this</p>

<pre><code>for event in events['items']:
    print "email1 = " + str(event['attendees'][0]['email'])
    print "email2 = " + str(event['attendees'][1]['email'])
</code></pre>

<p>and I can. And I've learned that lists in python <a href="http://stackoverflow.com/questions/13694034/is-a-python-list-guaranteed-to-have-its-elements-stay-in-the-order-they-are-inse">always have their order preserved</a>, which is convenient because I wanted to access the dictionaries inside the list with the index of the list. But what I've learned is that sometimes the <code>0</code> index refers to <code>email1</code> and sometimes it refers to <code>email2</code>. Why the inconsistency? Is it inherent to the google calendar api or is there something about having dictionary objects within a python list that relaxes the order preservation assumption? Or is it something else I'm missing?</p>

<p>So, as @Colonel Thirty Two pointed out, while lists preserve order, how google return data into a list may not be in the same order as it was submitted to them. This order inconsistency with attendees is inconvenient if you are wanting to count on that order for the retrieval of attendees with something like</p>

<pre><code>for event in events['items']:
    print "email1 = " + str(event['attendees'][0]['email'])
    print "email2 = " + str(event['attendees'][1]['email'])
</code></pre>

<p>What's more is that very few fields are writable with the google calendar api. What is writable, however, is <code>comments</code>. So, I added a value to that field to make the attendees identifiable. Like so</p>

<pre><code>'attendees': [
            {
                'email': agent_email,
                'displayName': agent_name,
                'comment': 'agent'
            },
            {
                'email': event_attendee_email,
                'displayName': event_attendee_name,
                'comment': 'client'
            },
</code></pre>

<p>Using <code>comment</code> as an identifier helped me in retrieving the <code>email</code> and <code>displayName</code> of each attendee with a simple if-statement.</p>

<pre><code>for i in range(len(event['attendees'])):
        if event['attendees'][i]['comment'] == 'client':
            event['attendees'][i]['displayName'] = event_attendee_name
            event['attendees'][i]['email'] = event_attendee_email
</code></pre>

<p>Now it doesn't matter that the google calendar api submits my attendees back to me in a different order than the one in which I added them. I can now retrieve the attendees so I can change them. Problem solved.</p>

python python-2.7 google-calendar