Python has a nice feature:
print([j**2 for j in [2, 3, 4, 5]]) # => [4, 9, 16, 25]
In Ruby it's even simpler:
puts [2, 3, 4, 5].map{|j| j**2}
but if it's about nested loops Python looks more convenient.
In Python we can do this:
digits = [1, 2, 3]
chars = ['a', 'b', 'c']
print([str(d)+ch for d in digits for ch in chars if d >= 2 if ch == 'a'])
# => ['2a', '3a']
The equivalent in Ruby is:
digits = [1, 2, 3]
chars = ['a', 'b', 'c']
list = []
digits.each do |d|
chars.each do |ch|
list.push d.to_s << ch if d >= 2 && ch == 'a'
end
end
puts list
Does Ruby have something similar?
Even better, some things that are hard in Ruby are one-liners in Python with list comprehensions. So if you are a Rubyist trying to learn Python, here are some of the basics of list comprehensions explained side by side with equivalent Ruby.
Ruby doesn't include a LinkedList class so we need to create our own. We want the following operations to be available: append (to the end of the list) append_after.
A Python list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element in the Python list. Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list.
Time Saved with List Comprehension. Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.
The common way in Ruby is to properly combine Enumerable and Array methods to achieve the same:
digits.product(chars).select{ |d, ch| d >= 2 && ch == 'a' }.map(&:join)
This is only 4 or so characters longer than the list comprehension and just as expressive (IMHO of course, but since list comprehensions are just a special application of the list monad, one could argue that it's probably possible to adequately rebuild that using Ruby's collection methods), while not needing any special syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With