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
python python-3.x numpy
add a comment |
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
python python-3.x numpy
add a comment |
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
python python-3.x numpy
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
python python-3.x numpy
python python-3.x numpy
edited Jan 1 at 4:26


AI_Learning
3,57721033
3,57721033
asked Dec 4 '18 at 21:29
MaryMary
134
134
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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 sliceend
is the index of the first element to be excluded from the slicesteps
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
add a comment |
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.
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it withoutrange
just with slices.
– Mikhail Stepanov
Dec 4 '18 at 21:53
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
add a comment |
Range starts from the 0 index if not otherwise specified. You want to use something like
for i in range(1,nI):
...
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53621696%2fhow-to-start-from-second-index-for-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 sliceend
is the index of the first element to be excluded from the slicesteps
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
add a comment |
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 sliceend
is the index of the first element to be excluded from the slicesteps
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
add a comment |
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 sliceend
is the index of the first element to be excluded from the slicesteps
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
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 sliceend
is the index of the first element to be excluded from the slicesteps
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
answered Jan 1 at 5:05
Luiz FerrazLuiz Ferraz
714
714
add a comment |
add a comment |
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.
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it withoutrange
just with slices.
– Mikhail Stepanov
Dec 4 '18 at 21:53
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
add a comment |
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.
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it withoutrange
just with slices.
– Mikhail Stepanov
Dec 4 '18 at 21:53
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
add a comment |
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.
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.
edited Dec 4 '18 at 21:47
answered Dec 4 '18 at 21:38


Mikhail StepanovMikhail Stepanov
1,5693812
1,5693812
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it withoutrange
just with slices.
– Mikhail Stepanov
Dec 4 '18 at 21:53
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
add a comment |
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it withoutrange
just with slices.
– Mikhail Stepanov
Dec 4 '18 at 21:53
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
My range is I=5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it without
range
just with slices.– Mikhail Stepanov
Dec 4 '18 at 21:53
Can you edit your post and add the missing part of your code snippet? Guess it's possible to solve it without
range
just with slices.– Mikhail Stepanov
Dec 4 '18 at 21:53
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
I did. I want i starts from 1 in my list. I don't want 0 for my for-loop
– Mary
Dec 4 '18 at 22:04
add a comment |
Range starts from the 0 index if not otherwise specified. You want to use something like
for i in range(1,nI):
...
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
|
show 1 more comment
Range starts from the 0 index if not otherwise specified. You want to use something like
for i in range(1,nI):
...
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
|
show 1 more comment
Range starts from the 0 index if not otherwise specified. You want to use something like
for i in range(1,nI):
...
Range starts from the 0 index if not otherwise specified. You want to use something like
for i in range(1,nI):
...
answered Dec 4 '18 at 21:36


Jack MoodyJack Moody
722723
722723
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
|
show 1 more comment
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
My range is 5 but I want to skip 1 from this range and start from i=2 in this range
– Mary
Dec 4 '18 at 21:47
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
So you want to get the second through fifth items in your list?
– Jack Moody
Dec 4 '18 at 21:51
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want my for loop get the index of the second value in the list and do the iteration to end
– Mary
Dec 4 '18 at 21:56
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
I want something like this: for i+1 in range(n)
– Mary
Dec 4 '18 at 21:57
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
Ok. Does the solution I gave work for you?
– Jack Moody
Dec 4 '18 at 21:58
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53621696%2fhow-to-start-from-second-index-for-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown