python-3.xlistautomationcoursera-api

can someone please explain this: whats the emailindex over here?


email_key = ' ' + 'Email Address'
email_index = user_data_list[0].index(email_key)

normally we take .index of the given list but here index0 is also included of another list afterwards .index(email_key) is there.its a question from coursers qwiklabs


Solution

  • It is important to understand the structure of the data, which is what leads to the error...

    Basically, this can be the case where there is a lists of lists (or at least the first index [0] of the list is an list itself).

    Here is an example of how this would work:

    # imagine a list that look like this...
    user_data_list = [
        ['hello Email Address','world Email Address', ' Email Address'], 
        200, 
        300, 
        True, 
        3.14159
        ]
    
    # this was the code in the original question
    email_key = ' ' + 'Email Address'
    email_index = user_data_list[0].index(email_key)
    print(email_index)
    
    
    # for clarity (with type hinting) it looks like this
    email_key = ' ' + 'Email Address'
    email_index:list = user_data_list[0]
    email_index.index(email_key)
    print(email_index)
    

    both examples would print 2 as a result.