Python For loop Inside List YASH PAL, 14 July 202514 July 2025 For loop inside a list in Python – In this article, we will understand how to use the for loop inside a list in Python programming. Let’s understand this concept step by step.Let’s say we have a list “squares_values = []” and we want to append some data. Let’s say we wish to add 10 square values to it. So, using the code, we can write like this.For loop inside the list in Pythonsquares_values = []for i in range(10): squares_values.append(i*i)Output - [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]The above code simply first declares the list and then, using the for loop, it appends the square values of 1 to 10 in the list. So Python is all about writing shortcodes. So the above code will be written in one line as shown below.squares_values = [i*i for i in range(10)]Output - [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]So here we are writing the same three lines above in one line, and the output is the same. So, how to use a for loop inside a list? So the syntax goes like this List_Name = [value [for loop condition]]Even if I can say there is any syntax over there because we can apply as many conditions as we want, or based on the condition. Let’s say we want to use an if condition inside a for loop, and that for loop is used for appending the data to the list. So instead of writing the code like this. squares_values = []for i in range(10): if i%2 == 0: squares_values.append(i*i)We can simply write the code like this.squares_values = [i*i for i in range(10) if i%2 == 0]So there is nothing different between the codes. This feature is similar to list comprehension.Other related tutorialsIntroduction to C programmingStudent Marksheet program in Python Computer Science Tutorials Python Tutorials Python