0
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

  • Hi Nida, this is quite common actually. it suggests that it might be a list of lists (or similar).... so element 0 of the first list is another list... – D.L Aug 18 '23 at 10:44

1 Answers1

0

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.

D.L
  • 4,339
  • 5
  • 22
  • 45