Pythonic way to iterate through a range starting at 1
range(1, n+1)
is not considered duplication, but I can see that this might become a hassle if you were going to change 1
to another number.
This removes the duplication using a generator:
for _ in (number+1 for number in range(5)):
print(_)
Loop starting from 0 or 1? Which one and why?
1) No
2) Neither
3) Arrays in C and C++ are zero-based.
4) Yes.
Start a loop from 1
Ruby supports a number of ways of counting and looping:
1.upto(10) do |i|
puts i
end
>> 1.upto(10) do |i|
> puts i
| end #=> 1
1
2
3
4
5
6
7
8
9
10
There's also step
instead of upto
which allows you to increment by a step value:
>> 1.step(10,2) { |i| puts i } #=> 1
1
3
5
7
9
How to start from second index for for-loop
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
Why is this for loop starting at index 1 instead of index 0?
When i=0
, range(i)
is empty, so it skips the inner loop:
>>> list(range(0))
[]
Skip first entry in for loop in python?
To skip the first element in Python you can simply write
for car in cars[1:]:
# Do What Ever you want
or to skip the last elem
for car in cars[:-1]:
# Do What Ever you want
You can use this concept for any sequence
(not for any iterable
though).
Related Topics
Mongoid: Find Through Array of Ids
How to Get the Unique Elements from an Array of Hashes in Ruby
Why Doesn't This Work If in Ruby Everything Is an Object
Verb-Agnostic Matching in Sinatra
Why Was the Object_Id for True and Nil Changed in Ruby2.0
Deploy with Capistrano Failing
Ruby: "&& Return" VS "And Return"
Most of My Assets Suddenly Return 404 After a Push to Heroku
Call Task More Than Once in Rails 3 Generator
Rails Sort Tags by Most Used (Tag.Posts.Count)
How to Ignore Multiline Comments in SASS
What Does the Operator ||= Stand for in Ruby
Ruby Create Recursive Directory Tree
Carrierwave - Resizing Images to Fixed Width
Escape Double and Single Backslashes in a String in Ruby
How to Add Child Nodes in Nodeset Using Nokogiri