Questions tagged [prepend]

Add content on to the beginning of something (usually a file).

497 questions
765
votes
10 answers

Append integer to beginning of list in Python

How do I prepend an integer to the beginning of a list? [1, 2, 3] ⟶ [42, 1, 2, 3]
gen
  • 9,528
  • 14
  • 35
  • 64
750
votes
8 answers

How do I prepend to a short python list?

list.append() appends to the end of a list. This explains that list.prepend() does not exist due to performance concerns for large lists. For a short list, how do I prepend a value?
hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
356
votes
11 answers

Most efficient way to prepend a value to an array

Assuming I have an array that has a size of N (where N > 0), is there a more efficient way of prepending to the array that would not require O(N + 1) steps? In code, essentially, what I currently am doing is function prependArray(value, oldArray) { …
samccone
  • 10,746
  • 7
  • 43
  • 50
231
votes
9 answers

.append(), prepend(), .after() and .before()

I am pretty proficient with coding, but now and then I come across code that seems to do basically the same thing. My main question here is, why would you use .append() rather then .after() or vice verses? I have been looking and cannot seem to…
Epik
  • 3,327
  • 4
  • 17
  • 23
196
votes
21 answers

Unix command to prepend text to a file

Is there a Unix command to prepend some string data to a text file? Something like: prepend "to be prepended" text.txt
One Two Three
  • 22,327
  • 24
  • 73
  • 114
160
votes
13 answers

How can I implement prepend and append with regular JavaScript?

How can I implement prepend and append with regular JavaScript without using jQuery?
Ben
  • 25,389
  • 34
  • 109
  • 165
125
votes
6 answers

Adding code to a javascript function programmatically

I'm attempting to customize an existing JS library without modifying the original JS code. This code loads in a few external JS files which I do have access to, and what I'd like to do is change one of the functions contained in the original file…
Munzilla
  • 3,805
  • 5
  • 31
  • 35
119
votes
8 answers

How to insert element as a first child?

I want to add a div as a first element using jquery on each click of a button
some text
some text
Rana Imtiaz
  • 1,363
  • 3
  • 12
  • 18
91
votes
14 answers

Prepend a line to an existing file in Python

I need to add a single line to the first line of a text file and it looks like the only options available to me are more lines of code than I would expect from python. Something like this: f = open('filename','r') temp = f.read() f.close() f =…
Nick
  • 9,792
  • 7
  • 50
  • 60
90
votes
5 answers

Prepend std::string

What is the most efficient way to prepend std::string? Is it worth writing out an entire function to do so, or would it take only 1 - 2 lines? I'm not seeing anything related to an std::string::push_front.
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
82
votes
4 answers

How to prepend int to slice

I have a slice which I created using var x []int; for i := 2; i < 10; i += 2 { x = append(x, i); } I want to prepend an integer to this slice, something like x = append(2, x) but obviously it won't work since append needs a slice as the first…
coda
  • 2,188
  • 2
  • 22
  • 26
82
votes
2 answers

D3.js prepend (similar to jQuery prepend)

I like the usage of append in D3, and I'm looking for prepend. Does this exist in D3?
Mia
  • 6,220
  • 12
  • 47
  • 81
61
votes
5 answers

What is the idiomatic way to prepend to a vector in Clojure?

Prepending to a list is easy: user=> (conj '(:bar :baz) :foo) (:foo :bar :baz) Appending to a vector is easy: user=> (conj [:bar :baz] :foo) [:bar :baz :foo] How do I (idiomatically) prepend to a vector, while getting back a vector? This does not…
0x89
  • 2,940
  • 2
  • 31
  • 30
53
votes
2 answers

What's the difference between `::` and `+:` for prepending to a list)?

List has 2 methods that are specified to prepend an element to an (immutable) list: +: (implementing Seq.+:), and :: (defined only in List) +: technically has a more general type signature— def +:[B >: A, That](elem: B)(implicit bf:…
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
49
votes
7 answers

jquery - keep window from changing scroll position while prepending items to a list?

I have a page that displays messages and I want it to work just like Facebook, but without the lazy loader. Messages are displayed in chronological order, most recent last. My message list is initially populated x number of most recent messages, and…
Redtopia
  • 4,947
  • 7
  • 45
  • 68
1
2 3
33 34