Js Lim
Js Lim

Reputation: 3695

Python 3 how to add a single value to multi dimension array column

Let say this is the original list

a = [['john', 12, 15],
     ['keith', 8, 20],
     ['jane', 18, 10]]

I want to add a value 99 to each row, the expected result will be like

a = [['john', 12, 15, 99],
     ['keith', 8, 20, 99],
     ['jane', 18, 10, 99]]

Any build in function to achieve this result?

Upvotes: 0

Views: 52

Answers (2)

Hannes Ovrén
Hannes Ovrén

Reputation: 21831

If you want to modify your existing lists then simply loop and append:

for l in a:
    l.append(99)

If you are fine with creating new lists then use the list-comprehension suggested by @languitar.

Upvotes: 4

languitar
languitar

Reputation: 6784

You can use list comprehension for this:

a = [x + [99] for x in a]

Btw. what you are using is a python list, not an array.

Upvotes: 1

Related Questions