is it safe to append to a list during iteration if i want to iterate over the added value?

  • Last Update :
  • Techknowledgy :

That said, I would probably use a while loop in this case, whether or not you want to have the entire list at the end.

current = 247
result_list = [current]

while current != 1:
   if current % 2 == 0:
   current /= 2
else:
   current = current * 3 + 1

result_list.append(current)

Though really I would probably use a generator.

def collatz(start):
   current = start

yield current

while current != 1:
   if current % 2 == 0:
   current /= 2
else:
   current = current * 3 + 1

yield current

Suggestion : 2

July 9, 2021July 9, 2021

The list append function does not return any value, it just adds the value to the list.

a = []
for i in range(5):
   a.append(i)
print(a)

within the loop to add object to list while iterating over the list.

list1 = ["a", "b", "c"]

list_length = len(list1)

for i in range(list_length):
   list1.append("New " + list1[i])

print(list1)

Suggestion : 3

Last Updated : 08 Feb, 2022

Syntax:

for (i = 0; i < list_name.size(); i++) {
   // code block to be executed
}

A
B
C
D

Syntax

ListIterator<data_type> variable = list_name.listIterator();

A
B
C
D

Suggestion : 4

In this syntax, the for loop statement assigns an individual element of the list to the item variable in each iteration.,In this example, the for loop assigns an individual element of the cities list to the city variable and prints out the city in each iteration.,Inside the body of the loop, you can manipulate each list element individually.,To access the index, you can unpack the tuple within the for loop statement like this:

To iterate over a list, you use the for loop statement as follows:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
for item in list:
   # process the itemCode language: Python(python)

For example, the following defines a list of cities and uses a for loop to iterate over the list:

cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico']

for city in cities:
   print(city) Code language: Python(python)

Output:

New York
Beijing
Cairo
Mumbai
MexicoCode language: Shell Session(shell)

To access the index, you can unpack the tuple within the for loop statement like this:

cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico']

for index, city in enumerate(cities):
   print(f "{index}: {city}") Code language: Python(python)

The following example uses the enumerate() function with the index that starts from one:

cities = ['New York', 'Beijing', 'Cairo', 'Mumbai', 'Mexico']

for index, city in enumerate(cities, 1):
   print(f "{index}: {city}") Code language: Python(python)