Hyun Mo Luke Yang
Hyun Mo Luke Yang

Reputation: 39

adding value to existing keys using python

Hi I am using Python to save the data of location using latitude, longitude, and timestamp(unix time). Using dictionary

But I tried to add dictionary using function with append, I cannot run it because "append" cannot work with str

Here is my code so far

dic_loc = {'latitude': 'Value for latitude ', 'longitude': 'Value for longitude', 'timestamp': 'Time Stamp'}



def make_timestamped_loc(La, Lng, Time):
    dic_loc['latitude'].append(La)
    dic_loc['longitude'].append(Lng)
    dic_loc['timestamp'].append(Time)

make_timestamped_loc(37.481236, 126.952733, 1483196400)
make_timestamped_loc(37.481126, 126.952733, 1498859012)

print(dic_loc)

If I can, I would like to eliminate the existed str value and adding the new value to each keys. Thank you very much

Upvotes: 0

Views: 1685

Answers (5)

Kaushik NP
Kaushik NP

Reputation: 6779

Append works only with list and so you can't use it in the dict as it stores string at the assignment. So, you can do it two ways. Use a list to store values and then append it or add to string itself.


Use list:

dic_loc = {'latitude': ['Value for latitude '], 'longitude': ['Value for longitude'], 'timestamp': ['Time Stamp']}

def make_timestamped_loc(La, Lng, Time):
    dic_loc['latitude'].append(La)
    dic_loc['longitude'].append(Lng)
    dic_loc['timestamp'].append(Time)

make_timestamped_loc(37.481236, 126.952733, 1483196400)
make_timestamped_loc(37.481126, 126.952733, 1498859012)

print(dic_loc)

'''
{
 'latitude': ['Value for latitude ', 37.481236, 37.481126],
 'longitude': ['Value for longitude', 126.952733, 126.952733], 
 'timestamp': ['Time Stamp', 1483196400, 1498859012]
}
'''

Or strings:

dic_loc = {'latitude': 'Value for latitude ', 'longitude': 'Value for longitude', 'timestamp': 'Time Stamp'}

def make_timestamped_loc(La, Lng, Time):
    dic_loc['latitude']+= ' '+str(La)
    dic_loc['longitude']+=' '+str(Lng)
    dic_loc['timestamp']+=' '+str(Time)

make_timestamped_loc(37.481236, 126.952733, 1483196400)
make_timestamped_loc(37.481126, 126.952733, 1498859012)

print(dic_loc)

'''
{
 'latitude': 'Value for latitude  37.481236 37.481126', 
 'longitude': 'Value for longitude 126.952733 126.952733', 
 'timestamp': 'Time Stamp 1483196400 1498859012'}
'''

If you are looking for just :

If I can, I would like to eliminate the existed str value and adding the new value to each keys.

You can directly assign the value :

def make_timestamped_loc(La, Lng, Time):
    dic_loc['latitude'] = La
    dic_loc['longitude'] = Lng
    dic_loc['timestamp'] = Time

Upvotes: 2

Skyy2010
Skyy2010

Reputation: 855

If you want to replace the actual timestamp with the newest one, I would go with this:

def make_timestamped_loc(La, Lng, Time):

    return {'latitude': 'Value for latitude ' + str(La), 'longitude': 'Value for longitude' + str(Lng), 'timestamp': 'Time Stamp ' + str(Time)}

As already stated by the other comments, .append(obj) does only work with list objects.

Upvotes: 0

mrCarnivore
mrCarnivore

Reputation: 5088

Leave the key empty at first and then append:

dic_loc = {'latitude': [], 'longitude': [], 'timestamp': []}

def make_timestamped_loc(La, Lng, Time):
    dic_loc['latitude'].append(La)
    dic_loc['longitude'].append(Lng)
    dic_loc['timestamp'].append(Time)

Upvotes: 1

SatanDmytro
SatanDmytro

Reputation: 537

Try:

dic_loc = {'latitude': 'Value for latitude ', 'longitude': 'Value for longitude', 'timestamp': 'Time Stamp'}


def make_timestamped_loc(lat, lng, timestamp):
    for key in ['latitude', 'longitude', 'timestamp']:
        if not isinstance(dic_loc[key], list):
            dic_loc[key] = [dic_loc[key]]

    dic_loc['latitude'].append(lat)
    dic_loc['longitude'].append(lng)
    dic_loc['timestamp'].append(timestamp)

make_timestamped_loc(37.481236, 126.952733, 1483196400)
make_timestamped_loc(37.481126, 126.952733, 1498859012)

print(dic_loc)

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

In your case, you should assign but not append:

...
dic_loc['latitude'] = La

Upvotes: 1

Related Questions