Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
my question is how to create a loop for x lists, informing how many lists you would like, example 3, and in print it gives the result list 1=[0, -1.5, -3.0, -4.5, -6.0, - 7.5 , -9, 0, -10.5] list 2=[0, -1.5, -3.0, -4.5, -6.0, -7.5, -9.0, -10.5] list 3 =[0, -1.5, -3.0, -4.5, -6.0, -7.5, -9.0, -10.5]

What I have tried:

u=0
car=3
list2 = [ ]
list2.append(0)
for j in range(car)

	for i in range(3,10):
	u+=(5/10)*-3
	list2.append(u)


print(list2)
Posted
Updated 31-Jul-22 22:00pm
Comments
Afzaal Ahmad Zeeshan 31-Jul-22 18:31pm    
And the problem is?
Rodrigo 2022 31-Jul-22 18:34pm    
my question is how to create a loop for x lists, informing how many lists you would like, example 3, and in print it gives the result list 1=[0, -1.5, -3.0, -4.5, -6.0, - 7.5 , -9, 0, -10.5] list 2=[0, -1.5, -3.0, -4.5, -6.0, -7.5, -9.0, -10.5] list 3 =[0, -1.5, -3.0, -4.5, -6.0, -7.5, -9.0, -10.5]
Afzaal Ahmad Zeeshan 31-Jul-22 18:42pm    
You can just use this "3" in a for loop to iterate over this value. But the code that you are showing is doing something else. It is "starting" from "3", not "ending" on it.

1 solution

You need to create a new sub-list inside list2 for each iteration of car, something like:
Python
u=0
car=3
list2 = []
for j in range(car):
    list2.append([]) # add a new sublist to list2

	for i in range(3,10):
	u += (5 / 10) * -3
	list2[j].append(u) # append the number to the current sublist


print(list2)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900