317

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
  • 2
    'shift' is pop and 'unshift' is push. In which shift takes in the number of parameters to pop
    – Arun
    May 14, 2013 at 10:19

11 Answers 11

457

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
  • 21
    Best answer since (a) evaluates to the trimmed array, and it works as expected on an empty array: [].drop(1) => [] Oct 21, 2013 at 0:28
  • 4
    Also the best answer because OP said "what's the easiest way to get rid of the first element"
    – notaceo
    Feb 8, 2014 at 6:21
  • 5
    This answer showcases that it is recommendable to scroll down even if the first and excepted answer has already more than 100 upvotes. Thank you. May 26, 2015 at 16:33
  • 7
    +1 since this returns a modified array, whereas shift mutates the array in place and returns the shifted element (or nil if array was empty)
    – yuval
    Jan 15, 2016 at 1:01
  • Folks, 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
    Nov 18, 2020 at 4:15
335

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
  • 3
    the explanation should be merged into the correct answer since the content is the same
    – Yolgie
    Sep 17, 2013 at 6:43
316

"pop"ing the first element of an Array is called "shift" ("unshift" being the operation of adding one element in front of the array).

2
  • 15
    Note this mutates the array and returns nil on an empty array. See drop for an alternative, as mentioned in the other answer.
    – Jay
    Oct 15, 2015 at 21:37
  • If mutating the same object, but there is a desire to have the return be the array, then the following will achieve this: array.tap(&:shift). Here is the documentation on tap: ruby-doc.org/3.3.0/Kernel.html#method-i-tap
    – Viet
    Feb 29 at 7:33
141
[0, 132, 432, 342, 234][1..]
=> [132, 432, 342, 234]

So unlike shift or slice, this returns a new array, keeping the original array untouched (useful for one liners).

6
  • 14
    One gotcha to watch out for: if the array is empty it returns nil: [][1..-1] => nil and not [].
    – Mohamad
    Jul 16, 2013 at 1:26
  • 3
    Isn't [1,2,3].shift a one-liner? Oct 2, 2013 at 23:50
  • 4
    @thekingoftruth: yes, but it evaluates to the element you threw away, not the rest of the array, so it takes another line. Oct 21, 2013 at 0:21
  • 4
    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. Oct 21, 2013 at 0:23
  • 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! Dec 1, 2013 at 13:02
117

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
  • 2
    very clever, and works perfectly. Once you do this, you'll have both variables "head" and "tail" at your disposal.
    – nfriend21
    Jul 18, 2013 at 16:25
  • 4
    This reminds me a lot of lisp. (let ((head (car mylist)) (tail (cdr mylist)) ...) Oct 22, 2013 at 9:45
  • @hurikhan77 Nice! I've been meaning to give it a try. Knowing that Darcs is written in Haskell helps pique my interest. Oct 23, 2013 at 23:24
  • 7
    This version also has the advantage of not mutating the original list Mar 17, 2014 at 10:19
  • 2
    useful when extracting info without an aditional step header, *properties = CSV.read(file_path,encoding: 'ISO-8859-1') Feb 15, 2017 at 17:23
18

or a.delete_at 0

18

Use shift method

array.shift(n) => Remove first n elements from array 
array.shift(1) => Remove first element

https://ruby-doc.org/core-2.2.0/Array.html#method-i-shift

15

You can use:

a.slice!(0)

slice! generalizes to any index or range.

6

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 returns nil if the index is out of range.
    – webninja
    Jan 5, 2018 at 17:41
6

You can use:

arr - [arr[0]]

or

arr - [arr.shift]

or simply

arr.shift(1)
1
  • arr - [arr[0]] this would fail for a = [1,2,3,4,5,6,7,8,1,2,3,4], because 1 is repeated.
    – vidur punj
    Jun 5, 2022 at 17:57
2

You can use:

 a.delete(a[0])   
 a.delete_at 0

Both can work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.