Skip to content

Instantly share code, notes, and snippets.

@katronai
Created March 3, 2020 22:44
Show Gist options
  • Save katronai/cd8d4eaf01c06acadd21c127cb3bee32 to your computer and use it in GitHub Desktop.
Save katronai/cd8d4eaf01c06acadd21c127cb3bee32 to your computer and use it in GitHub Desktop.
Looping in Python 3.
# Loop through list
for item in list:
print(item)
# Loop through list with index
for index, item in enumerate(list):
print(index, item)
# Loop through dictinary:
for key, value in dict.items():
print(key, value)
# Loop through Numpy array:
for val in np.nditer(np_array):
print(val)
# Loop through DataFrame: type(row): Series
for label, row in df.iterrows():
print(label)
print(row)
# Loop and decrease without being below a specific value (e.g. 0)
value = 30
for i in range(10):
value = max(0, value - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment