Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python change a key in a dictionary

# Basic syntax:
# Approach 1:
dictionary[new_key] = dictionary[old_key]
del dictionary[old_key]

# Approach 2:
dictionary[new_key] = dictionary.pop(old_key)
Comment

python replace by dictionary

address = "123 north anywhere street"

for word, initial in {"NORTH":"N", "SOUTH":"S" }.items():
    address = address.replace(word.lower(), initial)
print address
Comment

replace key of dictionary python

a_dict = {"a": 1, "B": 2, "C": 3}

new_key = "A"
old_key = "a"
a_dict[new_key] = a_dict.pop(old_key)

print(a_dict)
OUTPUT
{'B': 2, 'C': 3, 'A': 1}
Comment

change key of dictionary python

>>> dictionary = { 1: 'one', 2:'two', 3:'three' }
>>> dictionary['ONE'] = dictionary.pop(1)
>>> dictionary
{2: 'two', 3: 'three', 'ONE': 'one'}
>>> dictionary['ONE'] = dictionary.pop(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: 1
Comment

PREVIOUS NEXT
Code Example
Python :: kivymd window size 
Python :: binary, decimal, hex conversion python 
Python :: norm in python 
Python :: check how many times a substring appears in a string 
Python :: python open all files of type csv 
Python :: Python Format date using strftime() 
Python :: python multiple inheritance 
Python :: data structures and algorithms in python 
Python :: writerows to existing csv python 
Python :: drop a list of index pandas 
Python :: extract int from string python 
Python :: add a button pyqt5 
Python :: make zipfile from directory py 
Python :: python find largest variable 
Python :: install play sound python terminal 
Python :: python replace two spaces with one 
Python :: how to scrape multiple pages using selenium in python 
Python :: how to make addition in python 
Python :: keras declare functional model 
Python :: read csv pandas 
Python :: how to install api in python 
Python :: try except python not working 
Python :: how to remove quasi constant column in pandas dataframe 
Python :: tkinter button hide 
Python :: cassandra python 
Python :: python extract zip file 
Python :: pandas save dataframe to csv in python 
Python :: python read file into variable 
Python :: Determine the sum of al digits of n 
Python :: how to make a nan value in a list 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =