Home
Q&A
Education
Technology
Credit
General
Health
Insurance
Questions
Ask a Question
Add new keys to a dictionary?
Home
Education
Add new keys to a dictionary?
asked
Jan 21, 2022
in
Education
by
JackTerrance
How can I add a key in python directory after it's creation ? I already tried .add() method but it doesn't work. Select the correct answer from above options
dictionary
python-faq
python
add
1
Answer
0
votes
answered
Jan 21, 2022
by
JackTerrance
Best answer
Use this code to add multiple/single keys in a dictionary, for single key the code will have less computational overhead: >>> y = {2:3} >>> print y {2: 3} >>> q = {4:5, 6:7, 8:9} >>> y.update(q) >>> print y {2: 3, 4: 5, 6: 7, 8: 9} Hope this helps.
Related questions
0
votes
Q: How to return dictionary keys as a list in Python?
In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked
Feb 4, 2022
in
Education
by
JackTerrance
python
list
dictionary
0
votes
Q: Get key by value in dictionary
I made a function which will look up ages in a Dictionary and show the matching name: dictionary = {'george' ... it search backward. Select the correct answer from above options...
asked
Feb 4, 2022
in
Education
by
JackTerrance
python
dictionary
0
votes
Q: How can I sort a dictionary by key?
What would be a nice way to go from {2:3, 1:89, 4:5, 3:0} to {1:89, 2:3, 3:0, 4:5}? ... use the "sorted" operator that returns tuples. Select the correct answer from above options...
asked
Feb 2, 2022
in
Education
by
JackTerrance
python
sorting
dictionary
0
votes
Q: Delete an element from a dictionary
How can I delete an element from a dictionary? In order to return a copy, how can I delete an element/ ... modifying the original)? Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
pop
del
dictionary
0
votes
Q: Create a dictionary with list comprehension in Python
Can the Python list comprehension syntax be used to create dictionaries? For an example: By iterating over pairs of ... #doesn't work Select the correct answer from above options...
asked
Jan 22, 2022
in
Education
by
JackTerrance
python
dictionary
list-comprehension
0
votes
Q: How do I sort a list of dictionaries by a value of the dictionary?
I want each item to be sorted by a specific property value from a list of dictionaries I have. Take the ... When sorted by name Select the correct answer from above options...
asked
Jan 20, 2022
in
Education
by
JackTerrance
python
list
sorting
dictionary
data-structures
0
votes
Q: Check if a given key already exists in a dictionary
How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked
Jan 21, 2022
in
Education
by
JackTerrance
python
python-faq
directory
0
votes
Q: How do I sort a dictionary by value?
I have two fields a string field and a numeric field in a directory. The key of the dictionary as well as the ... based on the keys? Select the correct answer from above options...
asked
Jan 21, 2022
in
Education
by
JackTerrance
sorting
directory
python
python-faq
0
votes
Q: Python reverse / invert a mapping
Given a dictionary like so: my_map = { 'a': 1, 'b':2 } How can one invert this map to get: inv_map = { 1: 'a', 2: 'b' } Select the correct answer from above options...
asked
Feb 4, 2022
in
Education
by
JackTerrance
python
dictionary
0
votes
Q: Should I use 'has_key()' or 'in' on Python dicts?
I wonder what is better to do: d = {'a': 1, 'b': 2} 'a' in d True or: d = {'a': 1, 'b': 2} d.has_key('a') True Select the correct answer from above options...
asked
Jan 28, 2022
in
Education
by
JackTerrance
python
dictionary
0
votes
Q: Partial match between two python lists, one list exactly present in the other with few additional characters in Python
I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked
Jan 25, 2022
in
Education
by
JackTerrance
python
list
dictionary
list-comprehension
0
votes
Q: Iterating over dictionaries using 'for' loops
I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key ... Python? Or is it simply a variable? Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python
dictionary
python-keyword
0
votes
Q: How do you remove duplicates from a list whilst preserving order?
Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked
Jan 26, 2022
in
Education
by
JackTerrance
python
list
duplicate
python-faq
python-programming
0
votes
Q: What is the meaning of a single and a double underscore before an object name?
What is the meaning of using leading underscores before the name of an object in Python? And what's the difference ... , method, etc.? Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
python-faq
python
0
votes
Q: Replacements for switch statement in Python?
I want to write a program which returns different values for different input,it can be easily done using switch ... some alternative? Select the correct answer from above options...
asked
Jan 24, 2022
in
Education
by
JackTerrance
switch-statement
python-faq
python
...