Lists and Loops question

Hi all,

So I am following the Python 3 course on Codecademy. I am on the Lists section and I am trying out some code myself. I have made the following:

player_list = ["BOT Fred", "BOT Sam", "Player", "N2E", "Jet", "Jeff", "BOT 425", "Tim"]
BOT_List = []
for player in player_list:
  if player[0] == "B" and player[1] == "O" and player[2] == "T":

I am trying to figure out if I have done the correct thing. I wish to print only the items in player_list which start with the letters B then O then T to BOT_List. This seems to work with the above code, however I am confused about the meaning of player[0], player[1] and player[2].

I believe that the number in parentheses will refer to the whole element in the index. For example, print(player_list[0]) will return “BOT Fred”. If I am correct about this, what does player[0] refer to. Does it refer to the first letter in the first element in the list (letter B) or does it refer to the whole element (BOT Fred)?

This is my current logic about my code works (I am completely new to coding so hope this makes sense):
one by one, each element in player_list undergo the following:
1 - if the respective element(which we call player) in player_list starts with B and the second letter with O and the 3rd with T, then:
2- the code will add that element (player) to the BOT_List list

Once the loop is completed, the code will print the resulting BOT_List


you could do:

for player in player_list:
  print(player, player[0])

to gain some more information.

Once you have the elements of the list (strings), you can access the strings by index.

1 Like