Extract every n elements from an array (list) in Python and Ruby

Introduction

I usually write Ruby, but recently I started to touch Python as a hobby. I found it useful to know a slice of a Python list. By the way, I was wondering how to write ʻarray [:: 3]` in Python in Ruby, so I compared them.

Python

n = 3
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array[::n] #=> [0, 3, 6, 9]

Ruby

n = 3
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array.select.with_index { |_, i| i % n == 0 } #=> [0, 3, 6, 9]

Hmmm, is there a better way to write it ...: thinking:

Recommended Posts

Extract every n elements from an array (list) in Python and Ruby
Randomly select elements from list (array) in python
Tips: [Python] Randomly restore and extract an array from a fasta file
[Python] Manipulating elements in a list (array) [Sort]
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
[Python] Manipulation of elements in list (array) [Add / Delete]
Quicksort an array in Python 3
Getting list elements in Python
Sort and output the elements in the list as elements and multiples in Python.
Extract array elements and indexes in descending order with numpy
Extract multiple list duplicates in Python
Difference between list () and [] in Python
Delete multiple elements in python list
Extract strings from files in Python
Various ways to create an array of numbers from 1 to 10 in Python.
Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query
Differences between Ruby and Python in scope
Difference between append and + = in Python list
Download images from URL list in Python
[Python] Combine all the elements in the array
Correspondence summary of array operation of ruby and python
Receives and processes n objects in a list
Extract the product name and price from the product list in the Yodobashi.com purchase statement email.
Load and execute command from yml in python
Y / n processing in bash, python and Go
Sort list elements in a specified order in Python
Flatten an irregular 2D standard list in Python
How to remove duplicate elements in Python3 list
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
Pass an array from PHP to PYTHON and do numpy processing to get the result
Extract and list personal names and place names in the text
Implemented List and Bool in Python and SQLite3 (personal note)
AtCoder ARC080 D simulation solved in Ruby and Python
[Python beginner] Extract prefectures and cities from addresses (3 lines).
[Python] How to sort dict in list and instance in list
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
[Python] Extract only numbers from lists and character strings
Extract only elements that meet specific conditions in Python
Sorted list in Python
Ruby, Python and map
Python and Ruby split
List find in Python
Let's see how to count the number of elements in an array in some languages [Go, JavaScript, PHP, Python, Ruby, Swift]
I tried it with SymPyLive by referring to "[Ruby] Getting unique elements from an array".
Make sure all the elements in the list are the same in Python
How to slice a block multiple array from a multiple array in Python
List method argument information for classes and modules in Python
I got an error in vim and zsh in Python 3.7 series
Consider If Programming Was An Anime in Python and C
Get the number of specific elements in a python list
Useful tricks related to list and for statements in Python
Python: Create a dictionary from a list of keys and values
Predict gender from name using Gender API and Pykakasi in Python
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
AtCoder ABC168 A case expression solved in Ruby and Python
Extract the value closest to a value from a Python list element
Python on Ruby and angry Ruby on Python
Empty multidimensional array in python
OCR from PDF in Python
Python and ruby slice memo