1

I've stumbled upon this for loop. In the for loop, I can't understand i[:4] operation. The i variable will receive all the values from the 'cast' while the j variable will receive the corresponding index value. However, when I move down to the list2=i[:4], I can't get it. Does it mean to transfer the first four values in i? So i can contain ['A','B','C','D','E'], but it will only pass on the values from A to D to list2? Am I right?

for i,j in zip(movies['cast'],movies.index):
     list2=[]
     list2=i[:4]
     movies.loc[j,'cast']=str(list2)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
user234568
  • 741
  • 3
  • 11
  • 21
  • `i[:4]` this will slice and return all the elements from 0 (defaults to 0 when no number is given left of the colon) and until the 4th element (The 4 given is the index number, it is excluded, meaning all of 0-3). This will create a list of those 4 items. Note that you can also add steps to this in this manner: `list2 = i[:4:2]` which will take every 2nd element. – BoobyTrap Oct 11 '18 at 08:36

0 Answers0