How do you insert an item into a dictionary (in python 3.7.2)?

Michael Torrie torriem at gmail.com
Sun Jun 30 00:52:28 EDT 2019


On 06/28/2019 09:06 AM, CrazyVideoGamez wrote:
> How do you insert an item into a dictionary? For example, I make a dictionary called "dictionary".
> 
> dictionary = {1: 'value1', 2: 'value3'}
> 
> What if I wanted to add a value2 in the middle of value1 and value3?

How about:
dict[1.5] = 'value2'

In seriousness, though, it looks like you really want to be using a list
instead of a dict.  You can insert things into a list using the "insert"
method of the list.

a = ['value1', 'value3']
print (a[0])
a.insert(1,'value2')
print (a)






More information about the Python-list mailing list