Lets say I have an array
[0, 132, 432, 342, 234]
What is the easiest way to get rid of the first element? (0)
1
11 Answers
Use .drop(1)
.
This has the benefit of returning a new Array with the first element removed, as opposed to using .shift
, which returns the removed element, not the Array with the first element removed.
NOTE: It does not affect/mutate the original Array.
a = [0,1,2,3]
a.drop(1)
# => [1, 2, 3]
a
# => [0,1,2,3]
And, additionally, you can drop more than the first element:
[0,1,2,3].drop(2)
=> [2, 3]
[0,1,2,3].drop(3)
=> [3]
5
Best answer since (a) evaluates to the trimmed array, and it works as expected on an empty array:
[].drop(1) => []
Also the best answer because OP said “what’s the easiest way to get rid of the first element”
– notaceoThis answer showcases that it is recommendable to scroll down even if the first and excepted answer has already more than 100 upvotes. Thank you.
+1 since this returns a modified array, whereas
shift
mutates the array in place and returns the shifted element (ornil
if array was empty)– yuvalFolks, OP didn’t specify whether or not they wanted the operation to mutate the original array. So this answer is no better than the accepted answer of #shift. In fact, #shift is a better match for OP’s phrasing of “get rid of” IMO. A complete answer, however, would have provided both #shift and #drop and explained the difference.
– ryanc
Use the shift
method on array
>> x = [4,5,6]
=> [4, 5, 6]
>> x.shift
=> 4
>> x
=> [5, 6]
If you want to remove n starting elements you can use x.shift(n)
1
the explanation should be merged into the correct answer since the content is the same
– Yolgie
[0, 132, 432, 342, 234][1..-1]
=> [132, 432, 342, 234]
So unlike shift
or slice
this returns the modified array (useful for one liners).
6
One gotcha to watch out for: if the array is empty it returns nil:
[][1..-1] => nil
and not[]
.– MohamadIsn’t
[1,2,3].shift
a one-liner?@thekingoftruth: yes, but it evaluates to the element you threw away, not the rest of the array, so it takes another line.
I like this answer because it’s a one line expression that you can use anywhere. To avoid the [] vs nil problem, you can do
arry[1..-1] || []
. But arry.drop(1) is even better.I’m going with this cuz it’s simple, how would you get the “array minus what got shifted out”, I can’t see how that’d work… It’s the main answer but it doesn’t seem to answer the question since this next step isn’t obvious to a newbie!
This is pretty neat:
head, *tail = [1, 2, 3, 4, 5]
#==> head = 1, tail = [2, 3, 4, 5]
As written in the comments, there’s an advantage of not mutating the original list.
5
very clever, and works perfectly. Once you do this, you’ll have both variables “head” and “tail” at your disposal.
This reminds me a lot of lisp.
(let ((head (car mylist)) (tail (cdr mylist)) ...)
@hurikhan77 Nice! I’ve been meaning to give it a try. Knowing that Darcs is written in Haskell helps pique my interest.
This version also has the advantage of not mutating the original list
useful when extracting info without an aditional step
header, *properties = CSV.read(file_path,encoding: 'ISO-8859-1')
or a.delete_at 0
Use shift method
array.shift(n) => Remove first n elements from array
array.shift(1) => Remove first element
You can use Array.delete_at(0) method which will delete first element.
x = [2,3,4,11,0]
x.delete_at(0) unless x.empty? # [3,4,11,0]
1
I don’t think
unless x.empty?
is necessary. It simply returnsnil
if the index is out of range.– webninja
You can use:
arr - [arr[0]]
or
arr - [arr.shift]
or simply
arr.shift(1)
You can use:
a.delete(a[0])
a.delete_at 0
Both can work
‘shift’ is pop and ‘unshift’ is push. In which shift takes in the number of parameters to pop