Part of Github Copilot with Kotlin

How to use Copilot to create a large array of numbers

Sandy LaneSandy Lane

Video: How to use Copilot to create a large array of numbers by Taught by Celeste AI - AI Coding Coach

Take the quiz on the full lesson page
Test what you've read · interactive walkthrough

Python (and Kotlin): Create a Large Array of Numbers

Python: [i for i in range(1, 10001)]. Kotlin: (1..10000).toList(). The list comprehension and the range — different syntax, same idea.

This episode is the second outlier in the playlist (the first being episode 1, which was Swift) — Copilot was prompted in Python here, not Kotlin. The principles transfer.

The Copilot prompt (Python)

# Create a large array of numbers from 1 to 10000

Copilot generates:

# Using list comprehension to generate the array
numbers = [i for i in range(1, 10001)]

# Print the first 10 numbers to verify
print(numbers[:10])
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Walkthrough (Python)

1. List comprehension. [expr for item in iterable] — builds a list by evaluating expr once per item. Equivalent to a for-loop appending to a list, but more concise.

2. range(1, 10001). Python's range is exclusive on the upper bound. To include 10000, the upper bound is 10001.

3. The expression i. Just yields each item unchanged.

4. numbers[:10]. Python slice — first 10 elements.

The simpler form

numbers = list(range(1, 10001))

If the comprehension is just i, you don't need it — list(range(...)) is equivalent and more direct.

The comprehension form is for transformations:

squares = [i * i for i in range(1, 10001)]
even_doubled = [i * 2 for i in range(1, 10001) if i % 2 == 0]

These versions wouldn't simplify to list(...).

Equivalents in Kotlin

// 1..10000 as List<Int>
val numbers = (1..10000).toList()

// As IntArray (primitive, faster):
val ints = IntArray(10000) { it + 1 }

// With transformation
val squares = (1..10000).map { it * it }

// Filtered
val evens = (1..10000).filter { it % 2 == 0 }

Kotlin's range is inclusive on both ends — 1..10000 is 1 through 10000. No +1 needed for the upper bound.

IntArray(size) { initializer } is the constructor — primitive, doesn't box. For numerical data, faster.

Memory considerations

10,000 ints in a list: - Python: a list of 10,000 boxed Ints. Each Int is ~28 bytes overhead, plus the list's array of pointers. Roughly 280 KB. - Kotlin List<Int>: same — boxed Integer objects. Similar memory. - Kotlin IntArray: a single primitive int[]. 40 KB. 7× smaller.

For "really large" arrays (millions of elements), the primitive array matters.

Generating, not materializing

If you don't need the list, just iterate:

# Python — generator expression (no list built)
total = sum(i for i in range(1, 10001))
print(total)   # 50005000
// Kotlin — sequence (lazy)
val total = (1..10000).asSequence().sum()
// or simply (1..10000).sum() — IntRange has sum() built-in

For huge ranges, lazy evaluation avoids materializing the whole list.

Random numbers

For an array of 10,000 random numbers:

# Python
import random
numbers = [random.randint(1, 100) for _ in range(10000)]
// Kotlin
val numbers = IntArray(10000) { (1..100).random() }

Or for a deterministic seed:

val rng = kotlin.random.Random(seed = 42)
val numbers = IntArray(10000) { rng.nextInt(1, 101) }

Filtered and transformed in one shot

# All squares of multiples of 5, up to 10000
result = [i*i for i in range(1, 10001) if i % 5 == 0]
print(len(result))   # 2000 numbers
val result = (1..10000).asSequence()
  .filter { it % 5 == 0 }
  .map { it * it }
  .toList()

Both languages support the same patterns; the syntax is just different.

Common mistakes

Off-by-one with range. Python's range(1, 10000) is 1 through 9999. Use 10001 for inclusive 10000.

range(1, 10001) allocates immediately. Actually, range in Python 3 is lazy — it's an iterator-like object, not a list. list(range(...)) materializes it.

Generating instead of using range. [i for i in range(...)] and list(range(...)) produce the same thing. The first is verbose.

Using Array<Int> instead of IntArray in Kotlin. Boxes each int. Slower; more memory.

Memory blow-up. 10 million elements × boxing overhead = hundreds of MB. Use primitive arrays or sequences for huge counts.

What's next

Episode 39: How many days in this month? A LocalDate.now().lengthOfMonth() one-liner.

Recap

Python: [i for i in range(1, N+1)] or list(range(1, N+1)). Kotlin: (1..N).toList() or IntArray(N) { it + 1 }. Python's range is exclusive on the upper bound; Kotlin's .. is inclusive. For huge counts, Kotlin's IntArray is 5-10× more memory-efficient than List<Int>. For lazy iteration, Python uses generator expressions; Kotlin uses asSequence().

Next episode: days in this month.

Ready? Take the quiz on the full lesson page →
Test what you've learned. Watch the lesson and try the interactive quiz on the same page.