Python.v.JavaScript

JavaScript for Pythonistas. Python for JavaScripters

Lists / arrays

The basic structure to store several elements in a sequence is called list in Python and Array in JavaScript.

Python Javascript
Creation of list / array #
my_list = [3, 2, 10, "Hello"]
my_list
[3, 2, 10, 'Hello']
var my_array = [3, 2, 10, "Hello"];my_array;
[ 3, 2, 10, 'Hello' ]
Constructor-style creation of list / array #
my_list = list('Hello!')
my_list
['H', 'e', 'l', 'l', 'o', '!']
var my_array = Array(1, 2, 3, "Hello");my_array;
[ 1, 2, 3, 'Hello' ]
var my_array = new Array(1, 2, 3, "Hello");my_array;
[ 1, 2, 3, 'Hello' ]
var my_array = new Array(4);my_array;
[ <4 empty items> ]
var my_array = new Array("4");my_array;
[ '4' ]
var my_array = new Array("Hello!");my_array;
[ 'Hello!' ]

In JavaScript, one can use Array function to create an Array (with or without preceeding new). If this function receives an integer as the only argument, that integer is interpreted as the length of a newly created Array with empty slots. In other cases (several arguments, one string argument), the arguments become the elements of a newly created Array. Due to inconsistent behaviour of this function and Arrays with empty slots the usage of this method is strongly discouraged, see YDKJS for details.

Access by index #
my_list = [3, 2, 10, "Hello"]
my_list[0]
3
my_list[1]
2
my_list[-1]
'Hello'
var my_array = [3, 2, 10, "Hello"];my_array[0];
3
my_array[1];
2
my_array[my_array.length-1];
Hello
String indexes #
my_list = [2, 3, 10]
my_list["0"]
Exception: TypeError
list indices must be integers or slices, not str
var my_array = [2, 3, 10];my_array["0"];
2

In JavaScript, Arrays are Objects, and Objects are indexed by strings. Numeric indexes are coerced into strings.

Modification of the value of elements of list / array #
my_list = [3, 2, 10, "Hello"]
my_list[0] = 100
my_list
[100, 2, 10, 'Hello']
var my_array = [3, 2, 10, "Hello"];
my_array[0] = 100;my_array;
[ 100, 2, 10, 'Hello' ]
Length of list / array #
my_list = [3, 2, 10, "Hello"]
len(my_list)
4
var my_array = [3, 2, 10, "Hello"];my_array.length;
4
Appending element to list / array #
my_list = [3, 2, 10, "Hello"]
my_list.append(5)
my_list
[3, 2, 10, 'Hello', 5]
var my_array = [3, 2, 10, "Hello"];
my_array.push(5);my_array;
[ 3, 2, 10, 'Hello', 5 ]
Adding several elements in list / array #
my_list = [3, 2]
my_list.extend([12, 20])
my_list
[3, 2, 12, 20]
var my_array = [3, 2];
my_array.push(12, 20);my_array;
[ 3, 2, 12, 20 ]
Extending list / array with elements of another list / array #
my_list = [3, 2, 10, "Hello"]
other_list = [1, 2, 3]
my_list.extend(other_list)
my_list
[3, 2, 10, 'Hello', 1, 2, 3]
var my_array = [3, 2, 10, "Hello"];
var other_array = [1, 2, 3];
my_array.push.apply(my_array, other_array);my_array;
[ 3, 2, 10, 'Hello', 1, 2, 3 ]
reference
var my_array = [3, 2, 10, "Hello"];
var other = [1, 2, 3];
Array.prototype.push.apply(my_array, other);my_array;
[ 3, 2, 10, 'Hello', 1, 2, 3 ]
reference
function extend(arr, other) {
    /* you should include a test to 
     * check whether other_array really 
     * is an array */
    other.forEach(function(v) {
        arr.push(v);
    });
}
var my_array = [3, 2, 10, "Hello"];
var other_array = [1, 2, 3];
extend(my_array, other_array);my_array;
[ 3, 2, 10, 'Hello', 1, 2, 3 ]
reference

This is the most efficient way to extend an array in case the other_array is very large. In contrast with other methods discussed here, in this case we do not pass elements of other_array as arguments of a function.


var my_array = [3, 2, 10, "Hello"];
var other_array = [1, 2, 3];
my_array.push(...other_array);my_array;
[ 3, 2, 10, 'Hello', 1, 2, 3 ]
reference require: es6
Concatenation #
first_list = [1, 2, 3]
second_list = ["Hello", "World"]
new_list = first_list + second_list
new_list
[1, 2, 3, 'Hello', 'World']
first_array = [1, 2, 3];
second_array = ["Hello", "World"];
new_array = first_array.concat(second_array);new_array;
[ 1, 2, 3, 'Hello', 'World' ]
Remove array element by index #
my_list = [2, 10, 15]
del my_list[1]
my_list
[2, 15]
my_list[1]
15
len(my_list)
2
var my_array = [2, 10, 15];
delete my_array[1];my_array;
[ 2, <1 empty item>, 15 ]
my_array[1];
undefined
my_array.length;
3

See also splices.

Clear list / array #
my_list = [1, 2, 10]
my_list.clear()
my_list
[]
var my_array = [1, 2, 10]
my_array.length = 0my_array;
[]
Get element with index out of range #
my_list = [1, 20, 10]
my_list[3]
Exception: IndexError
list index out of range
var my_array = [1, 20, 10]my_array[3];
undefined
Add element out of range #
my_list = [1, 2, 10]
my_list[5] = 100
Exception: IndexError
list assignment index out of range
var my_array = [1, 2, 10]
my_array[5] = 100my_array;
[ 1, 2, 10, <2 empty items>, 100 ]
my_array[3];
undefined
my_array[5];
100
Operator in #
my_list = [1, 7, 10]
1 in my_list
True
7 in my_list
True
10 in my_list
True
2 in my_list
False
"1" in my_list
False

Operator in for lists checks if element exist in the list.


list_like_dict = {
    0: 1,
    1: 7,
    2: 10
}
1 in list_like_dict
True
7 in list_like_dict
False
10 in list_like_dict
False
2 in list_like_dict
True
"1" in list_like_dict
False

If one creates a list-like dictionary (with numeric keys), operator in checks whether an element exist among indexes (just like with any other dict). This is roughly like what JavaScript do.

var my_array = [1, 7, 10];1 in my_array;
true
7 in my_array;
false
10 in my_array;
false
2 in my_array;
true
"1" in my_array;
true

As Array is a subtype of object (i.e. something dict-like), in checks if given element exists among indexes (keys), not among values. See this answer for check among values.

Slices #
my_list = [0, 10, 20, 30, 40, 50, 60]
my_list[2:4]
[20, 30]
my_list[:2]
[0, 10]
my_list[2:]
[20, 30, 40, 50, 60]
my_list[2:-2]
[20, 30, 40]
my_list[:]
[0, 10, 20, 30, 40, 50, 60]
my_list[2:6:2]
[20, 40]
my_list[4:2:-1]
[40, 30]
my_array = [0, 10, 20, 30, 40, 50, 60]my_array.slice(2, 4);
[ 20, 30 ]
my_array.slice(null, 2);
[ 0, 10 ]
my_array.slice(2);
[ 20, 30, 40, 50, 60 ]
my_array.slice(2, -2);
[ 20, 30, 40 ]
my_array.slice();
[
   0, 10, 20, 30,
  40, 50, 60
]
reference

There's no slices with step (third argument in Python's) by default in JavaScript.

Splice modifications #
my_list = [0, 10, 20, 30, 40]
my_list[2:4] = [200, 300, 400]
my_list
[0, 10, 200, 300, 400, 40]
my_list[1:2] = []
my_list
[0, 200, 300, 400, 40]
my_list[3:3] = [999]
my_list
[0, 200, 300, 999, 400, 40]
var my_array = [0, 10, 20, 30, 40];
var deleted;
deleted = my_array.splice(2, 2, 200, 300, 400);my_array;
[ 0, 10, 200, 300, 400, 40 ]
deleted;
[ 20, 30 ]
my_array.splice(1, 1);my_array;
[ 0, 200, 300, 400, 40 ]
my_array.splice(3, 0, 999);my_array;
[ 0, 200, 300, 999, 400, 40 ]
reference

Method splice(start, deleteCount, item1, item2, ...) edits array in place: deletes deleteCount elements, beginning from start, then add elements item1, item2 and so on.

© Ilya V. Schurov and contributors, 2017
Licenses: CC BY (text), MIT (code).
contribute on github