Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

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 Python

squares_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 tutorials

  • Introduction to C programming
  • Student Marksheet program in Python
Computer Science Tutorials Python Tutorials Python

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes