Reputation: 3681
I am very new programming in Python, please bear with me.
I have a List of Lists like below :
[['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
['Test-Node3', 'Running', 'Test', '2231', 'machine3'],
['Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
And I wish to add few Items (at the beginning) to each of the sub-list. so It should be looking like below :
[['DOMAIN', 'Application' , 'Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
['UAT' , 'CaseCreation' , 'Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['UAT' , 'CaseCreation' , 'Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
['UAT' , 'CaseCreation' , 'Test-Node3', 'Running', 'Test', '2231', 'machine3'],
['UAT' , 'CaseCreation' , 'Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
['UAT' , 'CaseCreation' , 'Test-Node5', 'Running', 'Test', '2231', 'machine5']]
I would have tried reading SubList over a for loop and manipulate data into a CSV something, but is there a better way?
Please Suggest.
Upvotes: 0
Views: 1468
Reputation: 31
i think that the answers above would give you what you want. But, have you tried pandas library ? I think it would be a nice way to manage that kind of data. Look this example:
import pandas as pd
original_list = [['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
['Test-Node3', 'Running', 'Test', '2231', 'machine3'],
['Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
df_original= pd.DataFrame(original_list[1:], columns = original_list[0]) #Convert the list into a DataFrame
new_item = ["UAT","UAT","UAT","UAT","UAT"] #Create a list with the data, or a series
new_item2 = ["CaseCreation","CaseCreation","CaseCreation","CaseCreation","CaseCreation"]
df_original.insert(0,"DOMAIN",new_item) # Then you use insert to add the item wherever you want.
df_original.insert(1,"Application",new_item2)
print(df_original)
Output:
DOMAIN Application Name Status AppSpace MgmtPort Agent
0 UAT CaseCreation Test-Node4 Running Test 2231 machine1
1 UAT CaseCreation Test-Node1 Running Test 2231 Machine2
2 UAT CaseCreation Test-Node3 Running Test 2231 machine3
3 UAT CaseCreation Test-Node2 Running Test 2231 Machine4
4 UAT CaseCreation Test-Node5 Running Test 2231 machine5
With pandas you can manipulate each column and row as you want, even convert con CSV,EXCl in a easy way.
Upvotes: 2
Reputation: 12669
if you want you can use insert
method which allow to insert at any position in list:
list_a=[['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
['Test-Node3', 'Running', 'Test', '2231', 'machine3'],
['Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
a=['DOMAIN','Application']
b=['UAT' ,'CaseCreation']
for first,second in enumerate(list_a):
if first==0:
for item_1 in a:
second.insert(0, item_1)
else:
for item in b:
second.insert(0,item)
print(list_a)
Upvotes: 1
Reputation: 132
The iterative approach is what's needed, but if you wanted your code to be more concise, you can use the map() function from python, which allows you to perform element-wise operations to your list.
new = list(map( lambda x: ['Name', 'Status'] + x if(x[0] is 'Name') else ['UAT' , 'CaseCreation'] + x, oldlist ) )
Upvotes: 0
Reputation: 13651
You can insert items in the beginning of each sublist like below:
ar = [['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
['Test-Node3', 'Running', 'Test', '2231', 'machine3'],
['Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
first_row = ['DOMAIN', 'Application']
other_row = ['UAT' , 'CaseCreation']
for i in range(len(ar)):
if i==0:
for elem in first_row[::-1]:
ar[i].insert(0,elem)
else:
for elem in other_row[::-1]:
ar[i].insert(0,elem)
print(ar)
Output:
[['DOMAIN', 'Application', 'Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'], ['UAT', 'CaseCreation', 'Test-Node4', 'Running', 'Test', '2231', 'machine1'], ['UAT', 'CaseCreation', 'Test-Node1', 'Running', 'Test', '2231', 'Machine2'], ['UAT', 'CaseCreation', 'Test-Node3', 'Running', 'Test', '2231', 'machine3'], ['UAT', 'CaseCreation', 'Test-Node2', 'Running', 'Test', '2231', 'Machine4'], ['UAT', 'CaseCreation', 'Test-Node5', 'Running', 'Test', '2231', 'machine5']]
Upvotes: 1