• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

Python Day 4: Dictionaries

September 23, 2018 By Rowell Leave a Comment

Dictionaries

Indexes for dictionaries are called keys. Keys are associated with a value called a key-value pair. A dictionary is unordered. Compared to a List, there is no index starting at 0.

Dictionaries are typed with braces [python]{}[/python]. To assign a dictionary to the client1 variable:

[python]client1 = {'rssi': -52, 'channel': 36, 'noise': -96, 'tx rate': 600, 'mcs': 9}[/python]

The keys within this dictionary are rssi, channel, noise, tx rate, and mcs. The values for those keys are -52, 36, 96, 600, and 9, respectively.

To access a value through a key it is performed by:

[python]
client1[‘channel’]
36
[/python]

Trying to call a key that doesn’t exist in the dictionary will result in a KeyError error message.

[python]
client1['bssid']
Traceback (most recent call last):
File "", line 1, in 
KeyError: 'bssid'
[/python]

With methods, values can be returned from a dictionary. Let’s say I want to print out the key and value of client1. I’ll use a for-loop to iterate through each of the key-value pairs and print them out using the items() method.

[python]
for i in client1.items():
print(i)
('rssi', -52)
('channel', 36)
('noise', -96)
('tx rate', 600)
('mcs', 9)
[/python]

If we just needed to print out the keys we would use the keys() method. Again, I’ll use the for-loop to iterate through the keys and print them out:

[python]
for k in client1.keys():
print(k)
rssi
channel
noise
tx rate
mcs
[/python]

If we wanted just the values we can do the same for-loop but call the values() method:

[python]
for v in client1.values():
print(v)
-52
36
-96
600
9
[/python]

Share this:

  • Facebook
  • LinkedIn
  • Twitter

Related

Filed Under: Coding Tagged With: dictionaries, python

About Rowell

Wi-Fi expert. Coffee addict ☕️. Tech nerd. Business owner.

Reader Interactions

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.