kyoungwon cho
kyoungwon cho

Reputation: 67

How do I add values to a Python nested list?

How to add values ​​to each list?

before :

[['2010', '10', '24', '20', '32', '18', '5'], ['2020', '16', '22', '23', '30', '16', '9'], ['2030', '28', '19', '29', '30', '12', '13']]

result : [list] value add ''

[['2010', '10', '24', '20', '32', '18', '5', ''], ['2020', '16', '22', '23', '30', '16', '9', ''], ['2030', '28', '19', '29', '30', '12', '13', '']]

Upvotes: 2

Views: 88

Answers (2)

niraj
niraj

Reputation: 18208

You could also do following:

my_list = [['2010', '10', '24', '20', '32', '18', '5'], 
           ['2020', '16', '22', '23', '30', '16', '9'],
           ['2030', '28', '19', '29', '30', '12', '13']]

my_list = [inner_list + [''] for inner_list in my_list]
my_list

Output:

[['2010', '10', '24', '20', '32', '18', '5', ''],
 ['2020', '16', '22', '23', '30', '16', '9', ''],
 ['2030', '28', '19', '29', '30', '12', '13', '']]

Upvotes: 2

Ganesh
Ganesh

Reputation: 3428

Use for loop,

nested_list = [['2010', '10', '24', '20', '32', '18', '5'], ['2020', '16', '22', '23', '30', '16', '9'], ['2030', '28', '19', '29', '30', '12', '13']]

for a_list in nested_list:
    a_list.append('')

Upvotes: 3

Related Questions