I am new to python, and am setting up a large dataset to work on, and wanted to check and make sure that I am doing this in the most optimized manner possible, which right now I am pretty sure I am not. I have a python dictionary that is currently set up as so.
list1 = [1,2],[3,4],[4,5]
list2 = [10,20],[30,40],[50,60]
list3 = [100,200],[300,400],[400,500]
These list are created programmatically (and much larger in reality), and are then sorted into a dictionary as follows:
l_dict = {"l1":list1,"l2":list2,"l3":list3}
print l_dict
`{'l2': ([10, 20], [30, 40], [50, 60]), 'l3': ([100, 200], [300, 400], [400, 500]), 'l1': ([1, 2], [3, 4], [4, 5])}`
I have another dicitonary set up exactly the same, except with different numbers and object name (call it k_dict). Here are my questions: What is the simplest way to get an array/list of the first object in each tupple? My code now is as follows:
#Gets first tuple value from each list
listnames = ["l1","l2","l3"]
i=10
while(i < len(listdict)):
for x,a in l_dict[listnames[i]]:
return a[0]
i+=1
Which was working
Should return something like (1,3,4,10,30,50,100,300,400)
except spaced with newlines
I have also tried dict comprehension
print {x[0] for x in ldict[listnames]}
Which never worked for me, and I have tried many similar variations that wouldn't work either.
Also, is this dictionary a good way to set this data up? I couldn't find any other examples of list of tuples inside dictionaries, which leads me to believe there might be some dataframe/other manner of storing data like this that is easier to use. I am using python 2.7.1