Assign Values to Variables in a List Using a Loop in Python



In Python, lists are a useful way to store multiple elements in a single variable. To assign values to each entry in a list, loops are often needed. This strategy simplifies the process and improves the clarity of your code. This chapter will look at how to assign values to variables in a list using different loop types, using basic examples.

Using Simple Loop Iterations

In this method, we use the for loop to append elements to the lists. When we do not enter any element into the list and stop appending, then we just press the Enter key. To append the elements to the list, we use the append() method.

Example

The following is an example to assign values to the variables in a list using a loop in Python using the append() method.

L=[]
while True:
   item=input("Enter new item or press enter if you want to exit ")
   if item=='':
      break
   L.append(item)
print ("List : ",L)

When you run the program, it will show this output -

Enter new item or press enter if you want to exit 5
Enter new item or press enter if you want to exit 9
Enter new item or press enter if you want to exit 6
Enter new item or press enter if you want to exit
List : ['5', '9', '6']

Using the globals() Method

The globals() method retrieves the current global symbol table's dictionary. A symbol table is a data structure that a compiler maintains, which contains all of the program's necessary information.

Example

The following is an example to assign values to the variables in a list using a loop in Python using the globals() method.

var_names = ["one", "two", "three"]
count = 1
for name in var_names:
   globals()[name] = count
   count += 1
print(one)
print(two)
print(three)

After running the program, you will get this result -

1
2
3

Using List Comprehension

In this program, we will use list comprehension to create a list of even numbers.

# Using list comprehension to generate even numbers from 0 to 10
# includes i if it is even
even_numbers = [i for i in range(11) if i % 2 == 0]
print(even_numbers)

This output will be displayed when the program runs -

[0, 2, 4, 6, 8, 10]

Nested Loops to Create a Multiplication Table

In this example, we will use nested loops to create a multiplication table. The outer loop iterates through the numbers 1 to 10, and the inner loop multiplies each number by the current number of the outer loop.

# Creating an empty list for the multiplication table
multiplication_table = []

# Using nested loops to generate the table
for i in range(1, 6):  # Iterate through rows
   row = []  # Create an empty row
   # Iterate over columns
   for j in range(1, 6):  
      # Multiply and add to the current row  
      row.append(i * j)  
   # Append the created row to the table
   multiplication_table.append(row)  

print(multiplication_table)  

You will see this result after executing the program -

[[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20], [5, 10, 15, 20, 25]]
Updated on: 2025-04-23T18:02:45+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements