DEV Community

Arthur Christoph
Arthur Christoph

Posted on

Polyglot Series in Javascript, Dart, Ruby, Python, Go: Iterating List

Alt Text

Playground path: collection/list/iter

Iterating List

There are two common terminologies: Array and List

  • In Javascript and Ruby, it is called Array
  • In Python and Dart, it is called List
  • In spite of different terms, all of them have dynamic length - it will grow its length automatically, unlike Java's array or C's
  • In Go, array has fixed size, but it has Slice to declare array without specifying its length. We'll discuss more detail in my next posts.

In iteration, we can divide into 3 forms: element-only, index-only, or both index and current element.

  • Python and Dart use for-in a primary element-only iteration. Ruby's for-in is not as much used, as the .each method is more idiomatic in Ruby.
  • Javascript's for-in has a different purpose - enumerating object properties
  • do-while is not discussed below, as while is a more common form to use.

Javascript

Javascript has a for-of iteration to enumerate list.

let numbers = [1, 2, 3];

for (const e of numbers) {
  console.log(e);
}

When the iteration needs index, forEach has a second argument to display index, note that it cannot break out of the loop (using break) if a condition is met, unlike for-of.

numbers.forEach((e, i) => {
  console.log(e, i);
  // break;  this is an illegal statement
});

The versatile for loop with index, provides index-based iteration.

for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i], i);
}

while loop is more commonly used when there are multiple conditions, although it also possible to do the same thing with for loop.

let sum = 0;
while (sum >= 0 && sum < 3) {
  sum++;
}
console.log(sum);

//for loop alternative for while-like loop
for (let sum = 0; sum >= 0 && sum < 3; ) {
  sum++;
}
console.log(sum);

Dart

Dart uses for-in to iterate list.

  for (var e in numbers) {
    print(e);
  }

The usual for loop for index-based iteration.

for (var i = 0; i < numbers.length; i++) {
  print("${numbers[i]} ${i}");
}

while loop, more common for multi-conditions.

var sum = 0;
while (sum < 3) {
  sum++;
}
  print(sum);

Ruby

Ruby has for-in, but .each is more idiomatic in Ruby - the dot syntax.

for e in numbers do
  p e
end
numbers.each { |e| p e }

If index needed, each_with_index has a second argument representing the index.

numbers.each_with_index {|e,i| p "#{e} #{i}"}

Ruby's while method, just like others, is more common for multi-conditions.

sum = 0
while sum < 3
  sum += 1
end
p sum

Python

for-in method is the versatile loop syntax in Python. Unlike others we have seen above, instead of providing several for-like syntax, Python's for-in keeps a consistent form for element-only, index-only, or bot - by only modifying the form of the list to a suitable structure.

Without index, we can use for-in in its simplest form:

for e in [1, 2, 3]:
    print(e)

Range method returns list of sequential numbers, by passing the list's length calculated by len method, this is the for loop version of Python:

for i in range(len(numbers)):
    print(numbers[i], i)

For iterating both element and its index, enumerate method returns a pair of index and the current element. Note on the order - the index is the 1st argument and 2nd is the element unlike Javascript and Ruby 'index' method

for i, e in enumerate(numbers):
    print(e, i)

Python's while loop is just like others, used commonly for multi-conditions .

sum = 0
while sum < 3:
    sum += 1
print(sum)

Go

Go's for loop, is the only form of loop in Go, there's no while loop - focusing on minimum interface - fitting well with Go's idiom.

If index not needed, we use underscore to ignore it. The range keyword when passed a list, returns the index of the each element in the list.

for _, e := range numbers {
  fmt.Println(e)
}

We add i , to include index of the element.

for i, e := range numbers {
  fmt.Println(e, i)
}

The familiar for loop with index, also exists in Go.

for i := 0; i < len(numbers); i++ {
  fmt.Println(numbers[i])
}

Go avoids introducing while loop if for loop can do the same thing.

sum := 0
for sum < 3 {
  sum++
}
fmt.Println(sum)

Top comments (0)