Codementor Events

For...in Loops: Changing between Javascript and Python.

Published May 28, 2019Last updated May 29, 2019
For...in Loops: Changing between Javascript and Python.

If you have come to the point where you have to do Javascript in addition to Python, or Python in addition to Javascript, welcome to modern programming. It's the order of the day!

You can barely survive with only one language, as frameworks are proliferating and the technology ecosystem is seeing more and more overlaps. The lines between backend, frontend, and mobile continue to blur. So it's ok if you are combining Python and Javascript. However, if you are new to this combination, there are some subtle differences to note, else life can become hell.

For now, let's look at for...in loops.

For...in Loops

The for...in loop, equivalent to foreach...in in other languages, is a cool way of looping through every item in a sequence of items.

It is available in Javascript and Python, and might seem similar in how it is implemented...but! it behaves differently for each language.

Let's loop through a sequence of names of numbers in both languages, and display them one after the other.

Python 3.x

# Python Code

>>> numbers = ['one', 'two', 'three', 'four']
>>> for number in numbers:
... 	print(number)
one
two
three
four

Python prints out each item in the list. This makes for...in really cool.

Let's look at Javascript

Javascript

// Javascript Code

> var numbers = ['one', 'two', 'three', 'four'];
> for (var number in numbers){
...  console.log(number);
...}
0
1
2
3

There we go! Something different! Javascript displays the indices(or indexes) of the items, instead of the items themselves as we saw with python.

If we really want to get the values in Javascript, we need to add the extra code. We will use the indexes to get the items, and change the code a little bit for readability:

// Javascript Code

> var numbers = ['one', 'two', 'three', 'four'];
> for (var index in numbers){
... console.log(numbers[index]);
...}
one
two
three
four

Here we go!

Certainly, if you are moving from python to Javascript, note that we need to declare variables with keywords like var, let, const depending on the version, and the scoping preferences. We also have to use {..} for blocks, and append ; (semi-colon) to the end of logical lines.

And if you are coming from Javascript to Python, say goodbye to keywords like var, let, const. To declare the variable, you only need the name and its value. Blocks are formatted by indentation, using tabs or spaces - not both, and spaces recommended...and...Blocks also begin with logical lines ending in :(colon).

That was just a gist....

Back to code!

You may have asked yourself, by now, what to do if you needed the indexes of a list in python and not the items themselves.

Well, there's something for that too. It's called enumerate(). Let's use it:

# Python Code

>>> numbers = ['one', 'two', 'three', 'four']
>>>
>>> for index, number in enumerate(numbers):
... 	print(index)
0
1
2
3

There we go! The enumerate() function takes a list of items and numbers them. It then returns the items and their indexes as pairs; the first of the pair being the index of the item, and the second being the item itself. Read more here.

Definitely, you can get what each language achieves with for...in in different ways.

Takeaways

  1. When using for...in in Javascript, the default is that the index of each item is returned, except in key-value pairs (javascript objects).

  2. When using for...in in Python, the default behaviour is that the item itself is returned, except for key-value pairs...And that's for another post.

  3. You can add extra code to achieve something different for each language.

Always remember, they might appear similar, but they are not equal. 😃

...And keep tuned for the continuation...there are more iterables to explore for for...in.

Discover and read more posts from Victor Ayi
get started
post commentsBe the first to share your opinion
Yugkumar Bhaveshbhai Tandel
2 years ago

Thanks, it helped me.

Show more replies