What Does a for Loop Within a List Do in Python

What does a for loop within a list do in Python?

The line of code you are asking about is using list comprehension to create a list and assign the data collected in this list to self.cells. It is equivalent to

self.cells = []
for i in xrange(region.cellsPerCol):
self.cells.append(Cell(self, i))

Explanation:

To best explain how this works, a few simple examples might be instructive in helping you understand the code you have. If you are going to continue working with Python code, you will come across list comprehension again, and you may want to use it yourself.

Note, in the example below, both code segments are equivalent in that they create a list of values stored in list myList.

For instance:

myList = []
for i in range(10):
myList.append(i)

is equivalent to

myList = [i for i in range(10)]

List comprehensions can be more complex too, so for instance if you had some condition that determined if values should go into a list you could also express this with list comprehension.

This example only collects even numbered values in the list:

myList = []
for i in range(10):
if i%2 == 0: # could be written as "if not i%2" more tersely
myList.append(i)

and the equivalent list comprehension:

myList = [i for i in range(10) if i%2 == 0]

Two final notes:

  • You can have "nested" list comrehensions, but they quickly become hard to comprehend :)
  • List comprehension will run faster than the equivalent for-loop, and therefore is often a favorite with regular Python programmers who are concerned about efficiency.

Ok, one last example showing that you can also apply functions to the items you are iterating over in the list. This uses float() to convert a list of strings to float values:

data = ['3', '7.4', '8.2']
new_data = [float(n) for n in data]

gives:

new_data
[3.0, 7.4, 8.2]

Looping through a nested list and summing all elements of each internal list in python

summed_list = []
for sublist in All_slips:
summed_list.append(sum(sublist))

That's all. You can also employ a list comprehension.

summed_list = [sum(sublist) for sublist in All_slips]

How to run a nested loop in python inside list such that the outer loop starts from the next element of the list always and so on

Try this code !

For each iteration of outer loop, you need to iterate the inner loop from 1 increment to the range of list.

Code :

arr = [1,5,2,0,4,2,7]
for i in range(0,len(arr)):
print("Iteration # : ", i+1)
for j in range(i+1,len(arr)):
print("Outer loop value : " , arr[i] , " Inner loop value : " , arr[j])

Output :

Iteration # :  1                                                                                                       
Outer loop value : 1 Inner loop value : 5
Outer loop value : 1 Inner loop value : 2
Outer loop value : 1 Inner loop value : 0
Outer loop value : 1 Inner loop value : 4
Outer loop value : 1 Inner loop value : 2
Outer loop value : 1 Inner loop value : 7
Iteration # : 2
Outer loop value : 5 Inner loop value : 2
Outer loop value : 5 Inner loop value : 0
Outer loop value : 5 Inner loop value : 4
Outer loop value : 5 Inner loop value : 2
Outer loop value : 5 Inner loop value : 7
Iteration # : 3
Outer loop value : 2 Inner loop value : 0
Outer loop value : 2 Inner loop value : 4
Outer loop value : 2 Inner loop value : 2
Outer loop value : 2 Inner loop value : 7
Iteration # : 4
Outer loop value : 0 Inner loop value : 4
Outer loop value : 0 Inner loop value : 2
Outer loop value : 0 Inner loop value : 7
Iteration # : 5
Outer loop value : 4 Inner loop value : 2
Outer loop value : 4 Inner loop value : 7
Iteration # : 6
Outer loop value : 2 Inner loop value : 7
Iteration # : 7

How to create list inside for loop?

For this task I would choose a different data structure dict()

This will let You map the keys to respective lists such as:

my_dictionary = {}
for i in range(5):
my_dictionary[f"list_{i}"] = [] # This might be also my_dictionary["list_"+str(i)]

Please note that I have used empty list as key -> value mapping.

This in turn will produce a following dictionary:

{"list_0": [], "list_1": [], "list_2": [], "list_3":[], "list_4":[]}

how to convert nested for loops into list comprehension in python

Let's go one step at a time:

Step 1:

new_l=[]
for sublist in f_list:
for subsublist in sublist:
[new_l.append(i) for i in subsublist]

Step 2:

new_l=[]
for sublist in f_list:
[new_l.append(i) for subsublist in sublist for i in subsublist]

Step 3:

new_l = []
[new_l.append(i) for sublist in f_list for subsublist in sublist for i in subsublist]

And finally:

new_l = [i for sublist in f_list for subsublist in sublist for i in subsublist]

You might want to take a look at Flattening an irregular list of lists



Related Topics



Leave a reply



Submit