Skip to main content

Lists and Tuples

In Python, arrays are called lists and are used to store multiple consecutive values.

Task

Define a list of people that includes Bob, Tom, and Amy.

JavaScript implementation

let names = [];
names.push('Bob')
names.push('Tom')
names.push('Amy')
console.log(names)

Python implementation

names = [];
names.append('Bob')
names.append('Tom')
names.append('Amy')
print(names)

Code Highlight

  • Python uses my_list.append(el) to add elements to a list, while JavaScript uses myArr.push(el).

Difference Quick View

FeatureJavaScriptPython
Creationlet myArr = new Array();
let myArr = [1, 2];
let myTuple = [1, 2];
my_list = list()
my_list = [1, 2]
my_tuple = (1, 2)
Accesslet el = myArr[index];el = my_list[index]
AdditionmyArr.push(el);my_list.append(el)
Lengthlet length = myArr.length;length = len(my_list)
Slicinglet someEl = myArr.slice(start, end);some_el = my_list[start:end]
Concatenationlet mergedArr = myArr1.concat(myArr2);merged_list = my_list1 + my_list2
Copyinglet newArr = [...myArr];new_list = my_list1.copy()
ReversingmyArr.reverse();my_list.reverse()
DeletionmyArr.splice(index, 1);del my_list[index]
Maximum Valuelet maxVal = Math.max(...myArr);max_val = max(my_list)
Minimum Valuelet minVal = Math.min(...myArr);min_val = min(my_list)
Sumlet sumVal = myArr.reduce((a,b) => a + b, 0);sum_val = sum(my_list)
Conversion to Tuple-my_tuple = tuple(my_list);
What is a Tuple

A tuple can be understood as a read-only array. It determines the number of elements and their values at the time of creation and cannot be modified once created.

Resources