Python Forum
[SOLVED] Access keys and values from nested dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Access keys and values from nested dictionary?
#1
Question 
Hello,

I'm not used to what appears to be a nested dictionary in a list, and can't find how to get the keys and values:

for x in ifaddr.get_adapters():	print(x)	"""	Adapter(name='{FC153F79-475E-430D-B95A-06E966794B49}', nice_name='Realtek PCIe GbE Family Controller', ips=[IP(ip=('2a01:e0a:590:fd80:1a08:50b2:881d:2947', 0, 0), network_prefix=64, nice_name='Ethernet'), IP(ip=('fe80::6b43:963:5027:aacd', 0, 20), network_prefix=64, nice_name='Ethernet'), IP(ip='192.168.0.16', network_prefix=24, nice_name='Ethernet')], index=20)	"""	#AttributeError: 'Adapter' object has no attribute 'items'	for key, value in x.items() :	print(key)	#TypeError: 'Adapter' object is not iterable	#for y in x:	#AttributeError: 'Adapter' object has no attribute 'items'	"""	for y in x.items():	print(y + ':', x[y])	"""	#TypeError: 'Adapter' object is not subscriptable	#print(x[0])
I need to grab each adapter's nice_name. Does someone know?

Thank you.
Reply
#2
Hi,

what type of object is "Adapter". Based on the error message, it is obviously not a dictionary. You can check the type of an object by type(objectname). If I have to take a guess, it is an instance of a class.

And which name_name are you looking for? It appear twice, once top level and once inside the ips list. The one or the other way: there are no nested dictionaries in the output you show...

Regards, noisefloor
snippsat likes this post
Reply
#3
As mention over you not dealing with dictionary
ifaddr.get_adapters() returns a list of Adapter objects,and each Adapter has attributes like .nice_name .ip.
for adapter in get_adapters(): print(f"Adapter nice_name: {adapter.nice_name}") for ip in adapter.ips: # In real ifaddr, IPv6 is a tuple, IPv4 is a string, like in your example addr = ip.ip[0] if isinstance(ip.ip, tuple) else ip.ip print(f" IP:{addr} / {ip.network_prefix}")
Output:
Adapter nice_name: Realtek PCIe GbE Family Controller IP:2a01:e0a:590:fd80:1a08:50b2:881d:2947 / 64 IP:fe80::6b43:963:5027:aacd / 64 IP:192.168.0.16 / 24 Adapter nice_name: Wi-Fi IP:10.0.0.5 / 24
Reply
#4
All these are properties of object of class ifaddr.Adapter
You can access any of them using standard dot notation e.g. x.nice_name
Check the docs:
https://ifaddr.readthedocs.io/latest/#ifaddr.Adapter
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks all
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 178 Dec-18-2025, 01:54 AM
Last Post: DavidSah
  Replace values in Yaml file with value in dictionary PelleH 1 3,686 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 3,804 Jul-12-2024, 09:36 AM
Last Post: CAD79
Question Using Lists as Dictionary Values bfallert 8 3,888 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  How to access values returned from inquirer cspower 6 4,430 Dec-26-2023, 09:34 PM
Last Post: cspower
  need to compare 2 values in a nested dictionary jss 2 2,870 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 3,771 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Loop through json file and reset values [SOLVED] AlphaInc 2 7,412 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [Solved]Return values from npyscreen Extra 2 2,681 Oct-09-2022, 07:19 PM
Last Post: Extra
  [SOLVED] Concat data from dictionary? Winfried 4 3,483 Mar-30-2022, 02:55 PM
Last Post: Winfried

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.