How do I append a list to an existing dictionary in python

Hasen CH

I am trying to find dates 27 to 33 days apart from the starting date provided in the dictionary; however, I cannot append the new dates to the key-value combination pair. I keep getting an error that cannot assign to a function call. Any help would be appreciated.

dictionary_items = {"one": [20210101], "two": [20210202], "three": [20210303], "four": [20210404]}

dictionary_items = dictionary_items.items()

for key, value in dictionary_items:
    temp_var = []
    value = value[-1]
    temp_var.append(value)
    for date in range(20210101, 20210531, 1):
        x = (date - value) + 1
        if 27 <= x <= 33:
            temp_var.append(date)
            dictionary_items[key] = temp_var

print(dictionary_items)
            

Desired output :

{"one": [20210101, Date2, Date3], "two": [20210202, Date2, Date3], "three": [20210303, Date2, Date3],
 "four": [20210404, Date2, Date3]}
    
SorousH Bakhtiary

That's because you redefined dictionary_items label to it's .items() and you are trying to add item to items view. It's a just a view object of the original dictionary, you should add items to the dictionary itself:

dictionary = {"one": [20210101], "two": [20210202], "three": [20210303], "four": [20210404]}

for key, value in dictionary.items():
    temp_var = []
    value = value[-1]
    temp_var.append(value)
    for date in range(20210101, 20210531, 1):
        x = (date - value) + 1
        if 27 <= x <= 33:
            temp_var.append(date)
            dictionary[key] = temp_var

print(dictionary)

output :

{'one': [20210101, 20210127, 20210128, 20210129, 20210130, 20210131, 20210132, 20210133], 'two': [20210202, 20210228, 20210229, 20210230, 20210231, 20210232, 20210233, 20210234], 'three': [20210303, 20210329, 20210330, 20210331, 20210332, 20210333, 20210334, 20210335], 'four': [20210404, 20210430, 20210431, 20210432, 20210433, 20210434, 20210435, 20210436]}

Those temporary variables can be removed:

dictionary = {"one": [20210101], "two": [20210202], "three": [20210303], "four": [20210404]}

for key, value in dictionary.items():
    for date in range(20210101, 20210531):
        x = (date - value[0]) + 1
        if 27 <= x <= 33:
            dictionary[key].append(date)

print(dictionary)

output is the same.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I append a dictionary item to a pre-existing dictionary in Python?

How to append string to existing values in Python dictionary

How to append list of dictionary in python?

How can I open a file and append an item to an existing list in Python

How do I append a lambda to a list in python?

how do you append a list to a dictionary value in Python?

How do I convert list into dictionary in Python?

How do I append multiple lists to one key in a python dictionary?

How do I append values to a nested dictionary in Python?

How do i append a dictionary to a JSON file in python?

How do I insert a tuple into an existing dictionary in Python

Append a list of tuples into a existing dictionary

How to append the list of dictionary to same list in python?

How do I copy a list of lists and append it to a new list in python?

How to append a dictionary to a list correctly in Python

How to append an element to list that is in dictionary in Python

how to append item to dictionary value is a list python

how to add list values to existing dictionary in python

How do I append a list in python using a for loop and replace function?

How do I append a Python object attribute (list)

How do I append the index of a string to a list using extend() in python?

Python - How do I append numbers inside the list and

How to append a list to dictionary

How to append a dictionary value to an existing dictionary value?

How to add another dictionary entry to an existing dictionary to form a new dictionary (i.e. not append)

How do I use numbers in a list as keys for my dictionary in Python?

How do I return dictionary keys as a list in Python?

How do I find the last item of the list in a Python dictionary?

Python: How do I remove elements from a dictionary and return it as a list?