Answers for "how to put elements of for loop in list"

2

python assign list item in for loop

year = 2021
month_list = [1, 2, 3, 4, 5]
day = 1
date_list = []

for month in month_list:
    date_list.append( datetime(year, month, day).strftime("%Y-%m-%d") )

print(date_list)    
# ['2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01', '2021-05-01']
Posted by: Guest on May-21-2021
0

python how to loop in a list

fruits = ["apple", "banana", "strawberry"]

for fruit in fruits:
  print(fruit)
  #This "for fruit in fruits" gets every item in order from that list and does something with it
  #for example I print the fruit
  #note: This loop will only end when there aren't any items left in the list
  		 #example: After strawberry there isn't anything else soo the loop ends
#output: apple
		 #banana
  		 #stawberry
Posted by: Guest on October-13-2021

Code answers related to "how to put elements of for loop in list"

Python Answers by Framework

Browse Popular Code Answers by Language