nutship
nutship

Reputation: 4924

Inserting value to nested lists

I have two lists:

nums = ['-3.00', '-3.50', '-4.00']
values = [['1.9', ' 2.05'], ['1.97', ' 2.02'], ['2.03', ' 1.95']]

For every nested list in values I want to insert a number from nums.

The desired effect:

[['-3.00', '1.9', ' 2.05'], ['-3.50', '1.97', ' 2.02'], ['-4.00', '2.03', ' 1.95']]

I came up with:

[[row.insert(0, n) for n in nums] for row in values]

I wonder why this would not work.

Upvotes: 0

Views: 70

Answers (1)

jh314
jh314

Reputation: 27802

Reason why it doesn't work is that the insert method returns None.

You want this:

[[n] + v for n, v in zip(nums, values) ]

Upvotes: 3

Related Questions