I'm new to Solidity.

Does solidity have a method like range() to generate numbers within the range like [1:5] -> [1,2,3,4,5]? Or need to use for loop?

For example, a = list(range(1,21)) is what I used to do with Python, In Solidity, uint[20] SampleArray; SampleArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; this or need to use for loop by pushing numbers in a dynamic array would be the way? Would there be more simple way?

Also, I still don't get why people declare the value first and then assign later in Solidity.

2 Answers 2


No solidity does not have pre-defined functions such as range() which is present in python. To generate range, you have to use a for loop and push the data one by one to the array.

For your last line question, could you be more specific?


My friend gave me a great answer for this.

  • there's no function like that but on the low level that's no different than using a loop.

  • can push numbers in a dynamic array in a for loop but if we know how many entries, have unrolling the loop / hard coding each one and that will be cheaper

and for my last line, it was not Solidity what I saw Sorry for the confusion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.