Javascript & Python 1 liner list operations

If you often write in both Python and Javascript, then you might find this guide useful. I usually find myself knowing how to write certain operations in one language but not the other, so I created this to act as a cheat sheet.

Map

Python:

Suppose we have a list of dictionaries where each dictionary represents a person with a 'name' and 'age' field. We want to add a new field, 'adult', which indicates whether the person is an adult or not (age >= 18).

people = [{'name': 'Alice', 'age': 20}, {'name': 'Bob', 'age': 15}, {'name': 'Charlie', 'age': 25}]
people = list(map(lambda person: {**person, 'adult': person['age'] >= 18}, people))
print(people)  
# Output: [{'name': 'Alice', 'age': 20, 'adult': True}, {'name': 'Bob', 'age': 15, 'adult': False}, {'name': 'Charlie', 'age': 25, 'adult': True}]

Javascript:
We can do the same operation in JavaScript with an array of objects.

let people = [{name: 'Alice', age: 20}, {name: 'Bob', age: 15}, {name: 'Charlie', age: 25}];
people = people.map(person => ({...person, adult: person.age >= 18}));
console.log(people);
// Output: [{name: 'Alice', age: 20, adult: true}, {name: 'Bob', age: 15, adult: false}, {name: 'Charlie', age: 25, adult: true}]

Find

In Python, there's no built-in find() function, but you can achieve the same result using a list comprehension or generator expression:

numbers = [1, 2, 3, 4, 5]
found = next(x for x in numbers if x > 3)
print(found)  # Output: 4

In JavaScript, the find() method is called on an array and returns the first element that meets the condition:

let numbers = [1, 2, 3, 4, 5];
let found = numbers.find(x => x > 3);
console.log(found);  // Output: 4

Sort

In Python, you can use the sorted() function to sort an iterable:

numbers = [5, 2, 3, 1, 4]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # Output: [1, 2, 3, 4, 5]

In JavaScript, the sort() method is called on an array and mutates it in place. For numbers, you need to provide a compare function to sort them correctly:

let numbers = [5, 2, 3, 1, 4];
numbers.sort((a, b) => a - b);
console.log(numbers);  // Output: [1, 2, 3, 4, 5]

Reduce

In Python, you can use the reduce() function from the functools module:

from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce((lambda x, y: x * y), numbers)
print(product)  # Output: 120

In JavaScript, the reduce() method is called on an array:

let numbers = [1, 2, 3, 4, 5];
let product = numbers.reduce((a, b) => a * b); 
console.log(product); // Output: 120

Filter

The filter() function is used to create a list or array that passes a condition.

In Python, you can use filter() in conjunction with a function and an iterable. Here's an example where we filter out the odd numbers in a list:

numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4]

In JavaScript, filter() is a method that is called on an array. It also applies a function to every element in the array. Here's the JavaScript version of the Python example:

let numbers = [1, 2, 3, 4, 5];
let even_numbers = numbers.filter(x => x % 2 == 0);
console.log(even_numbers);  // Output: [2, 4]

Join

The join() function is used to combine all elements in a list or array into a single string with a specified delimiter.

In Python:

words = ['Hello', 'World']
sentence = ' '.join(words)
print(sentence)  # Output: "Hello World"

In JavaScript, join() is a method that is called on an array:

let words = ['Hello', 'World'];
let sentence = words.join(' ');
console.log(sentence);  // Output: "Hello World"

I hope you found these one-liners helpful, please leave a comment if you have some more ideas here.

Did you find this article valuable?

Support codingBites by becoming a sponsor. Any amount is appreciated!