R
R
Roman2021-06-07 17:31:47
Python
Roman, 2021-06-07 17:31:47

Add data to the dictionary with preservation, not interchange. Python?

Hello.
I am making a simple program - a glider.
Accordingly, the program asks - enter the task. If I use a list (where to add new tasks), I use the .append method, but I want to replace the lists with a dictionary so that each entry with the date of addition is displayed as a key and value.
In the case of a list, the new value is added to the end of the list, and in the case of a dictionary, the values ​​are interchanged
Question.
Is there a method for a dictionary, similar to .append for lists (so that the dictionary is formed from a set of tasks? I am attaching the relevant part of the code)

import time as t

HELP = '''\n● help - print help for the program.
● add - add a task to the list (the task name is requested from the user).
● show_d - tasks in dictionary'''

dic = {}
date = t.strftime ('%d.%m.%Y')

while True:
command = input ("\nEnter command: ")

elif command == "add ":
day = input ('''\nEnter task date:
4. Dictionary
''')
elif day == "4":
dic [date] = input ("\nNew task: ")
#dic.append (task_4)
print ("Task added")

elif command == "show_d":
print (dic)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kotar4ik, 2021-06-07
@Roma1167

Good afternoon.
You can create a dictionary with a list as value:

dic = {}

dic ["date"] = list(input ("\nНовая задача: "))
print (dic)



if "date" in dic:
    dic ["date"].append(input ("\nНовая задача: "))
    print (dic)

print('---------------')

print (dic['date'])
print (dic['date'][0])
print (dic['date'][1])

Новая задача: 4
{'date': ['4']}


Новая задача: 2
{'date': ['4', '2']}
---------------
['4', '2']
4
2

Try it, I think it will suit your task.

A
Alexander, 2021-06-07
@StupidQuestion

dict = {
   '1' : 'Один',
   '2' : 'Два'
}
print(dict) #{'1' : 'Один', '2' : 'Два'}

dict['3'] = 'Три'

print(dict) #{'1' : 'Один', '2' : 'Два','3':'Три'}

A
Antonio Solo, 2021-06-07
@solotony

don't just add, but first remove, and then add. then they will not be replaced but put at the end (if in python >= 3.6).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question