Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Put updated info under existing subheads instead of tagged onto the end where it might be missed
Source Link
pjz
  • 43.4k
  • 6
  • 54
  • 60

Creating an empty dictionary

Creating an empty dictionary

Creating a dictionary with initial values

Creating a dictionary with initial values

Inserting/Updating a single value

Inserting/Updating a single value

Inserting/Updating multiple values

Inserting/Updating multiple values

Creating a merged dictionary without modifying originalsPython 3.9+:

The update operator|= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionaryPython 3.5+:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

Python 3.9+:

The merge operator| now works for dictionaries:

data = data1 | {'c':3,'d':4}

Deleting items in dictionary

Check if a key is already in dictionary

Check if a key is already in dictionary

Iterate through pairs in a dictionary

Iterate through pairs in a dictionary

Create a dictionary from two lists

Create a dictionary from two lists


New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator|= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator| now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

Creating an empty dictionary

Creating a dictionary with initial values

Inserting/Updating a single value

Inserting/Updating multiple values

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

Check if a key is already in dictionary

Iterate through pairs in a dictionary

Create a dictionary from two lists


New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator|= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator| now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

Creating an empty dictionary

Creating a dictionary with initial values

Inserting/Updating a single value

Inserting/Updating multiple values

Python 3.9+:

The update operator|= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Python 3.5+:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

Python 3.9+:

The merge operator| now works for dictionaries:

data = data1 | {'c':3,'d':4}

Deleting items in dictionary

Check if a key is already in dictionary

Iterate through pairs in a dictionary

Create a dictionary from two lists

fix typo
Source Link
pjz
  • 43.4k
  • 6
  • 54
  • 60

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1 # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4}) # Updates 'c' and adds 'd'

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

del data[key] # Removes specific element in a dictionary
data.pop(key) # Removes the key & returns the value
data.clear() # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))

New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new featruefeature called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator |= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator | now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1 # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4}) # Updates 'c' and adds 'd'

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

del data[key] # Removes specific element in a dictionary
data.pop(key) # Removes the key & returns the value
data.clear() # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))

New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new featrue called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator |= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator | now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1 # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4}) # Updates 'c' and adds 'd'

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

del data[key] # Removes specific element in a dictionary
data.pop(key) # Removes the key & returns the value
data.clear() # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))

New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator |= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator | now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

Use better values for Python 3.9 examokes
Source Link
Rotareti
  • 54.7k
  • 24
  • 122
  • 115

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1 # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4}) # Updates 'c' and adds 'd'

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

del data[key] # Removes specific element in a dictionary
data.pop(key) # Removes the key & returns the value
data.clear() # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))

New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new featrue called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator|= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator| now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1 # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4}) # Updates 'c' and adds 'd'

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

del data[key] # Removes specific element in a dictionary
data.pop(key) # Removes the key & returns the value
data.clear() # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))

New to Python 3

Creating a merged dictionary without modifying originals

data = {**data1, **data2, **data3}

Feel free to add more!

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1 # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4}) # Updates 'c' and adds 'd'

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data) # Modifies data3, not data
data3.update(data2) # Modifies data3, not data2

Deleting items in dictionary

del data[key] # Removes specific element in a dictionary
data.pop(key) # Removes the key & returns the value
data.clear() # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))

New to Python 3.5

Creating a merged dictionary without modifying originals:

This uses a new featrue called dictionary unpacking.

data = {**data1, **data2, **data3}

New to Python 3.9

Update or add values for an existing dictionary

The update operator|= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

The merge operator| now works for dictionaries:

data = data1 | {'c':3,'d':4}

Feel free to add more!

Active reading [<https://en.wikipedia.org/wiki/History_of_Python#Version_3>]. Used more standard formatting.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
Loading
New Python3 options
Source Link
Yugal Jindle
  • 46k
  • 43
  • 136
  • 201
Loading
Loading
Added new formatting to iterate through keys in values in a dictionary.
Source Link
Remco Haszing
  • 8k
  • 7
  • 47
  • 90
Loading
Added new formatting to iterate through keys in values in a dictionary.
Source Link
Loading
Split massive code block to avoid the need to scroll inside this answer.
Source Link
Shadow
  • 9.5k
  • 4
  • 50
  • 60
Loading
Loading
Added dict comprehensions, which didn't exist when this answer was written.
Source Link
nmichaels
  • 51.2k
  • 12
  • 113
  • 137
Loading
Loading
Added "Creating a merged dictionary", other edits, comments, etc.
Source Link
jkdev
  • 11.8k
  • 15
  • 57
  • 79
Loading
Added "Creating a merged dictionary", other edits, comments, etc.
Source Link
jkdev
  • 11.8k
  • 15
  • 57
  • 79
Loading
added 47 characters in body
Source Link
Yugal Jindle
  • 46k
  • 43
  • 136
  • 201
Loading
Added additional information about dictionaries
Source Link
Loading
removed wrong statement, improved code block
Source Link
Marco Bonelli
  • 71.2k
  • 21
  • 130
  • 156
Loading
added 72 characters in body
Source Link
Marcin
  • 50.1k
  • 18
  • 137
  • 207
Loading
Source Link
Yugal Jindle
  • 46k
  • 43
  • 136
  • 201
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /