Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
••••••
IT NachrichtenBest Garmin watch deals to shop for October Prime Day 2026(29.09.2026 um 21:04 Uhr)
••
Hacking & PentestingBest Garmin watch deals to shop for October Prime Day 2026(29.09.2026 um 21:04 Uhr)
••••••••
IT NachrichtenBest Garmin watch deals to shop for October Prime Day 2026(29.09.2026 um 21:04 Uhr)
••
Hacking & PentestingBest Garmin watch deals to shop for October Prime Day 2026(29.09.2026 um 21:04 Uhr)
••
Intelligence View
⚡ tsecurity.de Intelligence

Dictionary in Python (1)

Buy Me a Coffee☕ *My post explains useful functions. You can create a dictionary as shown below. *dict() can also create a dictionary: v = {'name':'John', 'age':36} v = dict(name='John', age=36) v = dict([('name', 'John'), ('age', 3…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Buy Me a Coffee☕



*My post explains useful functions.



You can create a dictionary as shown below. *dict() can also create a dictionary:




v = {'name':'John', 'age':36}
v = dict(name='John', age=36)
v = dict([('name', 'John'), ('age', 36)])

v = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Tom', 'age':24}}
v = dict(person1=dict(name='John', age=36),
person2=dict(name='Tom', age=24))
v = dict([('person1', dict([('name', 'John'), ('age', 36)])),
('person2', dict([('name', 'Tom'), ('age', 24)]))])






You can access and modify a dictionary with a key in each [] as shown below. *A dictionary is mutable so you can modify a dictionary:



*Memos:





  • items() can get zero or more keys and values from a dictionary. *There are no arguments.


  • keys() can get zero or more keys from a dictionary. *There are no arguments.


  • values() can get zero or more values from a dictionary. *There are no arguments.




v = {'name':'John', 'age':36}
v = dict(name='John', age=36)
v = dict([('name', 'John'), ('age', 36)])

print(v['name'], v['age'])
# John 36

v['name'] = 'David'
v['gender'] = 'Male'

print(v)
# {'name': 'David', 'age': 36, 'gender': 'Male'}

print(v.items())
# dict_items([('name', 'David'), ('age', 36), ('gender', 'Male')])

print(v.keys())
# dict_keys(['name', 'age', 'gender'])

print(v.values())
# dict_values(['David', 36, 'Male'])









v = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v = dict(person1=dict(name='John', age=36),
person2=dict(name='Tom', age=24))
v = dict([('person1', dict([('name', 'John'), ('age', 36)])),
('person2', dict([('name', 'Tom'), ('age', 24)]))])

print(v['person1'], v['person2'])
# {'name': 'John', 'age': 36} {'name': 'Tom', 'age': 24}

print(v['person1']['name'], v['person1']['age'],
v['person2']['name'], v['person2']['age'])
# John 36 Anna 24

v['person1']['name'] = 'David'
v['person2']['gender'] = 'Female'
v['person3'] = {'name':'Tom', 'age':18, 'gender':'Male'}

print(v)
# {'person1': {'name': 'David', 'age': 36},
# 'person2': {'name': 'Anna', 'age': 24, 'gender': 'Female'},
# 'person3': {'name': 'Tom', 'age': 18, 'gender': 'Male'}}






You can use get() to get a value from a dictionary by a key as shown below:

*Memos:




  • The 1st argument is key(Required) for a key.

  • The 2nd argument is default(Optional-Default:None) to return a value when a key doesn't exist.

  • Don't use key= and default=.




v = {'name':'John', 'age':36}

print(v.get('name')) # John
print(v.get('gender')) # None
print(v.get('gender', "Doesn't exist!")) # Doesn't exist!









v = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
print(v.get('person2')) # {'name': 'Anna', 'age': 24}
print(v['person2'].get('name')) # Anna






You can use update() to get a value from a dictionary by a key as shown below:

*Memos:




  • The 1st argument is key(Required) for a key.

  • The 2nd argument is default(Optional-Default:None) to return a value when a key doesn't exist.

  • Don't use key= and default=.




v = {'name':'John', 'age':36}

v.update({'name':'David'})
print(v) # {'name': 'David', 'age': 36}









v = {'person1':{'name':'John', 'age':36},
'person2':{'name':'Anna', 'age':24}}
v.update({'person1':{'name':'David'}})
v['person2'].update({'name':'Tom'})
print(v) # {'person1': {'name': 'David'},
# 'person2': {'name': 'Tom', 'age': 24}}


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Dictionary in Python (1)

Thematisch verwandte Begriffe: Dictionary, Python · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-102714 | `_nx_icmpv6_validate_options()` scans the option area with `while (leng…
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag