Alex Rivera | Logout

What is the easiest way to push an element to the beginning of the array?

Asked 2011-05-22T01:42:27.830
244

I can't think of a one line way to do this. Is there a way?

Edit
Report

1 Answer

59

You can use insert:

a = [1,2,3]
a.insert(0,'x')
=> ['x',1,2,3]

Where the first argument is the index to insert at and the second is the value.

answered 2011-05-22T01:48:26.340

Your Answer