LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2019, 12:42 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Rep: Reputation: 2
Python. calling value from list of dictionaries


Code:
{"name": "code",
"version": "1.2",
"last_upload": 156,


"connections": [
{
   "name": "system2",
   "mode": "server",
   "state": "good",
   "status": "up",
   "timeout": 2
},
{
   "name": "system1",
   "mode": "server",
   "state": "good",
   "status": "up",
   "timeout": 2
}
]}
Using python how do I print or change values as
connections-->name
or connections-->satus

Thank you.
 
Old 07-19-2019, 01:02 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Code:
connections['name'] = 'system3'
print connections['name']
 
2 members found this post helpful.
Old 07-19-2019, 02:37 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Original Poster
Rep: Reputation: 2
Thank you!

how about the {} on outer most loop ?
So its probably dict.(lists(dict))
like dict in that dict. there is list named connections and in that list "connections" there are 2 dictionaries again.

this structure came about from command

Code:
values = json.loads(TMP)
          
          print
          print values
          print values["name"]
this prints "code"

I want to print and work on

value->connections->name

Last edited by PoleStar; 07-19-2019 at 02:38 PM.
 
Old 07-19-2019, 02:53 PM   #4
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by PoleStar View Post
Thank you!

how about the {} on outer most loop ?
So its probably dict.(lists(dict))
like dict in that dict. there is list named connections and in that list "connections" there are 2 dictionaries again.

this structure came about from command

Code:
values = json.loads(TMP)
          
          print
          print values
          print values["name"]
this prints "code"

I want to print and work on

value->connections->name
Just save values['connections'] in a variable, or use it directly in a for-loop.
It is a list, after all.
Code:
for v in values['connections']:
    v['name'] = 'new name'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find value in list of dictionaries in Python3 Mark_667 Programming 2 12-07-2018 09:21 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
python: differences between dictionaries and lists tjyorkshire Programming 5 08-12-2007 04:40 AM
Accessing nested dictionaries in Python DiagonalArg Programming 13 08-20-2005 08:37 PM
Python function arguments are dictionaries? johnMG Programming 1 07-24-2004 06:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration