Posts

Showing posts with the label dictionary

Searching a Python Dictionary with multiple values

Image
Searching a Python Dictionary with multiple values I have data in the following csv file, available here: http://s000.tinyupload.com/index.php?file_id=87473936848618674050 Screenshot of the CSV: I've written the following code to import the CSV file into Python as a Pandas Dataframe, and then the code after that creates a Dict. The dictionary has to have name and region as the keys, and the Windows and Linux prices as the dictionary values. #Import libraries and CSV file into dataframe, renaming columns, printing head import pandas as pd df = pd.read_csv('file.csv') col_names = ['Name','Region','API', 'Memory','vCPU', 'Storage', 'Linux', 'Windows' ] df.columns = col_names #Creating Dict dict = {} for i in df.index: key = (df.at[i, 'Name'] , df.at[i, 'Region']) #Rename columns accordingly value = (df.at[i, 'vCPU'], df.at[i, 'Memory'], df.at[i, 'Storage'], df.at[i, ...

Modifying list of dictionary using list comprehension

Modifying list of dictionary using list comprehension So I have the following list of dictionary myList = [{'one':1, 'two':2,'three':3}, {'one':4, 'two':5,'three':6}, {'one':7, 'two':8,'three':9}] This is just an example of a dictionary that I have. My question is, it possible to somehow modify say key two in all the dictionary to become twice their value, using list comprehension ? two I know how to use list comprehension to create new list of dictionary, but don't know how to modify them, I have come up with something like this new_list = { <some if condiftion> for (k,v) in x.iteritems() for x in myList } I am not sure how to specify a condition in the <some if condiftion> , also is the nested list comprehension format I am thinking of correct ? <some if condiftion> I want the final output as per my example like this [ {'one':1, 'two':4,'three':3...