Abi
Abi

Reputation: 363

How to start from second index for for-loop

I have this for-loop. I want i in range(nI) to start from the second number in the I list. Could you guide me?

I=[0,1,2,3,4,5,6]
nI=len(I)
for i in range(nI):
    sum=0
    for v in range(nV):
        for j in range(nJ):
            sum=sum+x1[i][j][v]
return sum

Upvotes: 24

Views: 144272

Answers (5)

Subham
Subham

Reputation: 411

  • You should avoid using names of built-ins. If you really must, add an underscore at the end e.g. sum_, but it's probably a hint that you should be using the built-in instead.
  • For iteration variables, it's recommended you usei, j, k, m, nunless you have a reason to name it something else.
  • If you are using numpy arrays, you can loop over all values using for value in np.nditer(...)
  • If you put sum = 0 inside of the loop, then it'll get reset to that every time.
  • You can "de-nest" for loops using itertools.product e.g.
sum(x[i][j][v] for i, j, v in itertools.product(range(nI), range(nJ), range(nV))) 
  • To reduce multiple for loops, Itertools has a product function that gives you a generator that will allow you to flatten multiple iterables into one iterable of tuples that effectively is the same as nested for loops. The only difference is ordering of the values.
from itertools import product

for x, y in product(range(3), range(4, 7)):
    print(x, y)
0 4
0 5
0 6
1 4
1 5
1 6
2 4
2 5
2 6

[Program finished]

Upvotes: 0

Mastermind
Mastermind

Reputation: 645

You can simply use slicing:

for item in I[1:]:
    print(item)

And if you want indexing, use pythonic-style enumerate:

START = 1
for index, item in enumerate(I[START:], START):
    print(item, index)

Upvotes: 23

Luiz Ferraz
Luiz Ferraz

Reputation: 1525

First thing is to remember that python uses zero indexing.

You can iterate throught the list except using the range function to get the indexes of the items you want or slices to get the elements.

What I think is becoming confusing here is that in your example, the values and the indexes are the same so to clarify I'll use this list as example:

I = ['a', 'b', 'c', 'd', 'e']
nI = len(I) # 5

The range function will allow you to iterate through the indexes:

for i in range(1, nI):
    print(i)
# Prints:
# 1
# 2
# 3
# 4

If you want to access the values using the range function you should do it like this:

for index in range(1, nI):
    i = I[index]
    print(i)
# Prints:
# b
# c
# d
# e

You can also use array slicing to do that and you don't even need nI. Array slicing returns a new array with your slice.
The slice is done with the_list_reference[start:end:steps] where all three parameters are optional and:
start is the index of the first to be included in the slice
end is the index of the first element to be excluded from the slice
steps is how many steps for each next index starting from (as expected) the start (if steps is 2 and start with 1 it gets every odd index).
Example:

for i in I[1:]:
    print(i)
# Prints:
# b
# c
# d
# e

Upvotes: 17

Mikhail Stepanov
Mikhail Stepanov

Reputation: 3790

If you want to iterate through a list from a second item, just use range(1, nI) (if nI is the length of the list or so).

for i in range(1, nI):
    sum=0
    for v in range(nV):
        for j in range(nJ):
            sum=sum+x1[i][j][v]

Probaly, a part of your function just lost somewhere, but anyway, in in general range() works like this:

range(start_from, stop_at, step_size)

i. e.

for i in range(2, 7, 2):
    print(i, end=' ')

Out:
2 4 6

Edit

Please, remember: python uses zero indexing, i.e. first element has an index 0, the second - 1 etc.

By default, range starts from 0 and stops at the value of the passed parameter minus one. If there's an explicit start, iteration starts from it's value. If there's a step, it continues while range returns values lesser than stop value.

for i in range(1, 7, 2):
    print(i, end=' ')

Out: 
1 3 5  # there's no 7!

Detailed description of range build-in is here.

Upvotes: 5

Jack Moody
Jack Moody

Reputation: 1771

Range starts from the 0 index if not otherwise specified. You want to use something like

for i in range(1,nI):
    ...

Upvotes: 2

Related Questions