banner

© 2004-21 ict4u.net

Using a 2D List (Array)

Using python to create a 2D list of data entered by the user
# monitor the cost of 5 items purchased over 4 weeks

# Create a list.
allCosts = []

# Append empty lists in first two indexes.


# Loop over rows.
for week in range(5):
	# Loop over columns.
	# now create a list for this week
	print("Week {}".format(week+1))
	costThisWeek = []
	for item in range(4):
		itemCost = float(input("Enter the cost of item  " + str(item+1)))
		costThisWeek.append(itemCost)
	# now append the cost of this weeks items to the whole list
	allCosts.append(costThisWeek)

# Display entire list.
print(allCosts)