The printed value of Python vector of numbers is displayed in a buffer:

[0.00000024 0.00000024 0.00000024 0.00000024 0.00000024 0.00000023
 0.00000024 0.00000024 0.00000024 0.00000024 0.00000023 0.00000024
 0.00000023 0.00000024 0.00000023 0.00000024 0.00000024 0.00000023
 0.00000023 0.00000023]

The goal is to append a comma to each written numeral except the last in the sequence. The resulting text should look like this:

[0.00000024, 0.00000024, 0.00000024, 0.00000024, 0.00000024, 0.00000023,
 0.00000024, 0.00000024, 0.00000024, 0.00000024, 0.00000023, 0.00000024,
 0.00000023, 0.00000024, 0.00000023, 0.00000024, 0.00000024, 0.00000023,
 0.00000023, 0.00000023]

The only idea that I have been able to come up with is mapping the vector, converting each number to a string, concatenating a comma to the end of each string (except the last), and then convert all of the strings to symbols .... I suspect there is a better approach.

  • 2
    What type should the end result be? A string? Something that is inserted into a buffer? "convert all string to symbols" doesn't make much sense. Mar 9, 2022 at 10:40
  • @Lindydancer -- The result is a Python list / array and the goal is to pass that as an argument when calling the script, e.g., python my-script.py ARRAY The problem is that Emacs treats the comma differently than other languages such as Python. My second thought is to have the Python script convert the data into a usable list / array, but I was hoping that Emacs could prepare the data and just pass it as an argument.
    – lawlist
    Mar 9, 2022 at 15:14
  • Based on the answers, the question is apparently not at all about either list or vector Lisp objects, but is about editing text in a buffer - text that represents a Python vector/list. I edited the question accordingly.
    – Drew
    Mar 9, 2022 at 17:35
  • @Drew: it's actually not clear where the original "vector" came from. My assumption was that it was actually a Lisp vector (whether a "real" vector or its string representation is not clear from the question). So it may (or may not) need to be converted to a string before it's edited to add the commas and then pass the modified string as an argument to the python program somehow (but again unspecified in the question).
    – NickD
    Mar 9, 2022 at 18:45
  • 1
    Note for Python a list or array can have a trailing comma
    – mmmmmm
    Mar 10, 2022 at 9:35

4 Answers 4


You can use mapconcat to transfer the elements to strings and to insert the separator.

(defun my-format-as-python-list (list)
  (concat "["
          (mapconcat (lambda (element)
                       (number-to-string element))
                     ", ")

(my-format-as-python-list '(1.0 2.45)) ;; =>   "[1.0, 2.45]"

Select those four lines, then type C-S-5 (to run query-replace), then replace “ ” with “, ”. Hit ! to accept all replacements. Put commas at the ends of the lines (read the help for regex-replace if you want to do it fast, but it may be faster to type three commas). If you do it a lot, make a keyboard macro.


What about using query-replace-regexp to replace \([0-9]\)\(^J? \) (where ^J is C-q C-j) with \1,\2?


You can enable calc-embedded mode on this vector by C-x * e. To display eight-digit fixed-point numbers (if needed) type d f 8 RET. Then C-x * e to return to the initial mode.

You should probably apply fill-paragraph to spruce up the result

So the keys sequence could be:

C-x * e d f  8 RET C-x * e M-q

Assuming the point within the square brackets. You can save this sequence as a keyboard macro.

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.