Home > Blockchain >  How to update values in Python's dictionary?
How to update values in Python's dictionary?

Time:04-24

My dictionary looks like below, and I am following this link to update the values in "Column_Type" key. Bascially, I would like to replace values "String" with "VARCHAR(256)", DATE with "NUMBER (4,0)", Int with "NUMBER" and Numeric with "Number". Whenever I run below code, my values are not getting updated to my dictionary.My desired output for updated dictionary is as below

Please note: The location of column_types might vary as well. For ex: Column_type[String] currently is at position 1, but It might be at position 3 later on .

{'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['String', 'Numeric', 'Date', 'Int']}

Code:

for key1, key2 in my_dict.items():
    if key2== 'String':
        my_dict[key2] = "VARCHAR(256)"
print(my_dict)

Desired Output:

{'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['VARCHAR(256)', 'NUMBER', 'NUMBER(4,0)', 'NUMBER']}

CodePudding user response:

In your example, your keys are "Column_Name" and Column_Type". There is no key named "String" in your dict. Both values in your dict are of type list so neither are equal to the string String either.

What you want is to replace a specific value in a list.

Try like this:

for index, value in enumerate(my_dict["Column_Type"]):
    if value == "String":
        my_dict["Column_Type"][index] = "VARCHAR(256)"

This replaces the value in the list, not the dict. That is what you want.

If you need to replace multiple values you can use a dict, like @Jeremy suggested:

type_strs = {
    'String': 'VARCHAR(256)',
    'Numeric': 'NUMBER',
    'Date': 'NUMBER(4,0)',
    'Int': 'NUMBER'
}

for index, value in enumerate(my_dict["Column_Type"]):
    my_dict["Column_Type"][index] = type_strs.get(value, value)

Here, the .get() function on a dict returns the value corresponding to the key given by the first argument, or the second argument if no such key exists.

CodePudding user response:

type_strs = {
    'String': 'VARCHAR(256)',
    'Numeric': 'NUMBER',
    'Date': 'NUMBER(4,0)',
    'Int': 'NUMBER'
}
my_dict['Column_Type'] = [type_strs[t] for t in my_dict['Column_Type']]

I would recommend a dictionary instead of if statements for translating the type strings

CodePudding user response:

Your are in this line comparing a list with an element of this list if key2== 'String':

key2 when you are traveling the variable contains the next ['String', 'Numeric', 'Date', 'Int'], so you will need to join to this value of the array for compare. You can do it with a for cycle

The program is the next:

my_dict={'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['String', 'Numeric', 'Date', 'Int']}

# We create this variable to save the position of the element
position=0

# We travel to the dictionary
for i in my_dict['Column_Type']:

    # If the variable is equal to the string
    if i == 'String':

        # We assign the new information to the variable
        my_dict['Column_Type'][position]="VARCHAR(256)"

    #And add one to the position
    position =1
    
print(my_dict)

Output

{'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['VARCHAR(256)', 'Numeric', 'Date', 'Int']}

CodePudding user response:

You can use list.update(val1, val2)

example:

# Dictionary of strings to ints
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 43
}
# Adding a new key value pair
word_freq.update({'before': 23})
print(word_freq)

CodePudding user response:

for key1, key2 in my_dict.items():
    if key2[0] == 'String':
        key2[0] = 'VARCHAR(256)'
    if key2[1] == 'Numeric':
        key2[1] = 'NUMBER'
    if key2[2] == 'Date':
        key2[2] = 'NUMBER(4,0)'
    if key2[3] == 'Int':
        key2[3] = 'NUMBER'

print(my_dict)

Output:

{'Column_name': ['Name', 'Salary', 'NUMBER(4,0)', 'Phone'], 'Column_Type': ['VARCHAR(256)', 'NUMBER', 'NUMBER(4,0)', 'NUMBER']}
  • Related