SHARE
TWEET

Iterating through list

Cobble5tone Aug 18th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dates = [1982, 1980, 1973, 2000]
  2.  
  3.  
  4. for date in dates:  # Iterates through dates list item by item until reaching 1973
  5.     if date == 1973:
  6.         break  # Exits loop upon finding 1973 value
  7.     else:
  8.         print(date)
  9.  
  10.  
  11. counter = 0
  12. year = 0  # Initialize year value
  13.  
  14. while year != 1973:  # Begins conditional loop
  15.    
  16.     print(dates[counter])  # Uses the counter as an index to print our indexed value in dates list
  17.     year = dates[counter]  # Sets the year value to the current indexed value in the dates list
  18.     counter += 1  # Advances the index 1 value per iteration
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top