Foreground
Foreground
D0Z - motion / graphic / dev๐Ÿงบ

Brain ๐Ÿ”’

โœ•

brain

CAUTION

Some files are private or not ready yet, if a link gives you a 404 that's why :)
This is a work in progress, new notes will be added daily

List of note Indexes


                .... |\| .|||.    |\/|  .|\\. .|.
              .\\||/|.  .\|..||  .\|..|||...||.....
            .|   .....||.|\\\\\\    ... |\\\/. .\\. .||
        .\\|   .|\\|..|\\ .\\\\  .\\|. .\\\\\|  |\| |\\|
        |\\  |\\|..  /\\\. |\|\\   \\\\|.   .|\\. .\\\\|
        .\\. |\\\\\\\|.. .\\|      .\\||\\/|. |\\|     .|/\|
      \. .\\\||/\|....   \\| |\/   |\\\  |\\\| .\\|  \\| ....
    |\/  |\\| |\\\\\\\| |\|.\\\   \\\\. |\\\\| |\\  \\.  ||..
      ..|||\|  .. .\\\\. |\\\\\\  .\\|....|\\\. |\\\\\\  |/. |/
    \\\|..   .\\\ |\\. .\\\| \\|   |||\\\.  ...|\\/||.  .\\\
  |\|  |\/.  /\\\\\| .\\\\. \\.   .../|.   .|\\\|.   |\\\|  |\\
    .  |||\\|.  ..||| .\\|.  |\|  \\|..../\\| |\\|  .\\\\\   ..
    \\|  .||\|.|..      |\\\|./\  \\||\. ||...    .\\\\/.  |\/||
    ..|/||.   |\\\\\|.     .|/\|  ||.        .||\\\     .|||. |\
  .\/|  \\\. .|\\\|||\\\||..       .|\\\\\\\|\\\\.  /\\\\||. |
    .|| |\\\\|  .......|\\\\\\.  .\\\. ..|||....  .\\\\|.. ...
    \\||....|\\\\\\\\||\\\\\\\/   |\|....|||\\\\\\\/....|\\\|
      ||.|\||\\| .....|\\\\..\\|    .|\\\. ||........ .|\|...
          . |\  /\\/\\\\\\. |\.  .\\\\\\.  ./\\\\/  |\\\||.
        .|\.|\| .|\|... ..              ./\\|..|\\|  ..
            .|\\|  .\\\\.\\\\\\\   \||...\\\|..|\\\|.||
                |\. ..||....||     ... ..||\\\. .|.
                        ...            .......

โ†‘ Python


Python - Dictionaries

A dictionary is an unordered collection of values using key: value pairs. Each key needs to be immutable and unique. Keys can use string or numbers, values can be of any type including mutable data such as lists or even dictionaries.

[!NOTE] As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

my_dictionary = {
  'name': 'John',
  'age': 25,
  'city': 'Montpellier'
}

[!TIP]

  • Difference with objects: An object is a class and can contain functions, a dictionary cannot.
  • Difference with lists: A list is an ordered collection of values that can be accessed using their index, a dictionary is an unordered collection of values that can be access using their key.

Keys and values

name = my_dictionary['name'] # 'John'
  • keys() method returns a view containing all the keys
  • values() method returns a view containing all the values
my_dictionary.keys() # dict_keys(['name', 'age', 'city'])
my_dictionary.values()  # dict_values(['John', 25, 'Montpellier'])

Add an entry

To add a new key:value pair you can add a value to a new key, if the key already exists it will be updated.

my_dictionary['job'] = "developer"

Remove an entry

# using del
del my_dictionary['age']
# or using pop to return the deleted value
my_dictionary.pop('city')

Check if a key exists

'name' in my_dictionary # True

Other methods

  • update(): Updates the dictionary with the specified key-value pairs
  • clear(): Removes all the elements from the dictionary
  • get(<key>, <default>): Returns the value of the specified key, if the key does not exist no error will be raised and a default value is returned if provided
  • copy(): Returns a copy of the dictionary
my_dictionary.update({'country': 'France', 'phone': '00000000'})
my_dictionary.get('age')
my_dictionary.get('first_name', 'Doe')
my_dictionary_copy = my_dictionary.copy()
my_dictionary.clear()

Append to dictionary of arrays

my_dictionary = {
  'a': ['hello', 'world'],
  'b': ['hello']
}

my_dictionary['b'].append('world')
my_dictionary['c'].append('hello') # Throws an error
my_dictionary['c'] = ['world'] # Overwrite c if it exists

If a c key already exists it will be overwritten to prevent this we could do:

if 'c' not in my_dictionary:
  my_dictionary['c'] = []

my_dictionary['c'].append('world')

but there is a better way:

The Default Dict

from collections import defaultdict
my_dictionary = defaultdict(list)

my_dictionary['c'].append('world') # {'c': ['world']})
my_dictionary['d'] # {'c': ['world'], 'd': []})

Dictionary Comprehensions

my_list = [('a', 'hello'), ('b', 'world')]
my_dict = {item[0]: item[1] for item in my_list}
# or even better with unpacking:
my_dict = {key: value for key, value in my_list}
print(my_dict) # {'a': 'hello', 'b': 'world'}

Items

my_dict = {'a': 'hello', 'b': 'world'}
my_dict.items() # dict_items([('a', 'hello'), ('b', 'world')])
list(my_dict.items()) # [('a', 'hello'), ('b', 'world')]
ยฉ 2024 d0z.eu | 0.1.0-beta.39