Reputation: 585
If I have a Python list:
my_list = [['Apples',10],['Bananas',15],['Oranges',2]]
... how can I convert it to a 3 dimensional list to:
my_list = [['Apples',10,0],['Bananas',15,0],['Oranges',2,0]]
This is a normal Python list (without using numpy).
Upvotes: 0
Views: 110
Reputation: 1985
Fancy and readable approach:
my_list = [['Apples',10],['Bananas',15],['Oranges',2]]
for item in my_list: item.append(0) # loop through each element
print(my_list) # output: [['Apples', 10, 0], ['Bananas', 15, 0], ['Oranges', 2, 0]]
Upvotes: 4
Reputation: 244
[x.append(0) for x in my_list]
or as commented by @Sushanth
[x+[0] for x in my_list]
Upvotes: 1