How to print specific keys and values from list of dict's on one line in python - Stack Overflow

admin2025-05-01  2

This is my list of dictionaries, which is part of a larger dictionary containing numerous lists

competitorRankDetails = [
        {
          "phaseCode": "M1",
          "phaseName": "Moto 1",
          "phaseBlockCode": "M1",
          "rank": 1,
          "raceName": "46",
          "racePosition": 6,
          "result": 1,
          "score": 1,
          "hillTime": "2.754",
          "time": "30.755"
        },
        {
          "phaseCode": "M2",
          "phaseName": "Moto 2",
          "phaseBlockCode": "M2",
          "rank": 1,
          "raceName": "97",
          "racePosition": 7,
          "result": 1,
          "score": 2,
          "hillTime": "2.732",
          "time": "30.689"
        },
        {
          "phaseCode": "M3",
          "phaseName": "Moto 3",
          "phaseBlockCode": "M3",
          "rank": 1,
          "raceName": "148",
          "racePosition": 3,
          "result": 1,
          "score": 3,
          "hillTime": "2.745",
          "time": "30.692"
        }
]

This is the print statement I've tried to get the output I'm looking for

 info = ['phaseName', 'racePosition', 'hillTime']                        
                        
                        for record in item['competitorRankDetails']:
                          print(*record,[record[k] for k in info])

This results in the following output

phaseCode phaseName phaseBlockCode rank raceName racePosition result score hillTime time ['Moto 1', 7, '2.883']
phaseCode phaseName phaseBlockCode rank raceName racePosition result score hillTime time ['Moto 2', 5, '2.882']
phaseCode phaseName phaseBlockCode rank raceName racePosition result score hillTime time ['Moto 3', 2, '2.887']

Is there a way I could have the output like below with the possibility to add more items to info

phaseName: Moto1 , racePosition: 7 , hillTime: 2.878
PhaseName: Moto2 , racePosition: 6 , hillTime: 2.856
PhaseName: Moto3 , racePosition: 1 , hillTime: 2.885


                        

This is my list of dictionaries, which is part of a larger dictionary containing numerous lists

competitorRankDetails = [
        {
          "phaseCode": "M1",
          "phaseName": "Moto 1",
          "phaseBlockCode": "M1",
          "rank": 1,
          "raceName": "46",
          "racePosition": 6,
          "result": 1,
          "score": 1,
          "hillTime": "2.754",
          "time": "30.755"
        },
        {
          "phaseCode": "M2",
          "phaseName": "Moto 2",
          "phaseBlockCode": "M2",
          "rank": 1,
          "raceName": "97",
          "racePosition": 7,
          "result": 1,
          "score": 2,
          "hillTime": "2.732",
          "time": "30.689"
        },
        {
          "phaseCode": "M3",
          "phaseName": "Moto 3",
          "phaseBlockCode": "M3",
          "rank": 1,
          "raceName": "148",
          "racePosition": 3,
          "result": 1,
          "score": 3,
          "hillTime": "2.745",
          "time": "30.692"
        }
]

This is the print statement I've tried to get the output I'm looking for

 info = ['phaseName', 'racePosition', 'hillTime']                        
                        
                        for record in item['competitorRankDetails']:
                          print(*record,[record[k] for k in info])

This results in the following output

phaseCode phaseName phaseBlockCode rank raceName racePosition result score hillTime time ['Moto 1', 7, '2.883']
phaseCode phaseName phaseBlockCode rank raceName racePosition result score hillTime time ['Moto 2', 5, '2.882']
phaseCode phaseName phaseBlockCode rank raceName racePosition result score hillTime time ['Moto 3', 2, '2.887']

Is there a way I could have the output like below with the possibility to add more items to info

phaseName: Moto1 , racePosition: 7 , hillTime: 2.878
PhaseName: Moto2 , racePosition: 6 , hillTime: 2.856
PhaseName: Moto3 , racePosition: 1 , hillTime: 2.885


                        
Share Improve this question edited Jan 3 at 8:41 simon 5,7111 gold badge16 silver badges29 bronze badges asked Jan 2 at 22:15 PintglassPintglass 295 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 4

You could make use of join and f-strings:

competitorRankDetails = [
        {
          "phaseCode": "M1",
          "phaseName": "Moto 1",
          "phaseBlockCode": "M1",
          "rank": 1,
          "raceName": "46",
          "racePosition": 6,
          "result": 1,
          "score": 1,
          "hillTime": "2.754",
          "time": "30.755"
        },
        {
          "phaseCode": "M2",
          "phaseName": "Moto 2",
          "phaseBlockCode": "M2",
          "rank": 1,
          "raceName": "97",
          "racePosition": 7,
          "result": 1,
          "score": 2,
          "hillTime": "2.732",
          "time": "30.689"
        },
        {
          "phaseCode": "M3",
          "phaseName": "Moto 3",
          "phaseBlockCode": "M3",
          "rank": 1,
          "raceName": "148",
          "racePosition": 3,
          "result": 1,
          "score": 3,
          "hillTime": "2.745",
          "time": "30.692"
        }
]

info = ['phaseName', 'racePosition', 'hillTime']                        
for record in competitorRankDetails:
    print(', '.join(f"{k}: {record[k]}" for k in info))

Output:

phaseName: Moto 1, racePosition: 6, hillTime: 2.754
phaseName: Moto 2, racePosition: 7, hillTime: 2.732
phaseName: Moto 3, racePosition: 3, hillTime: 2.745

So, it doesn't make sense to use print(*info), that's just going to print all the keys. YOu already have a list of keys you are interested in. So you could do something like:

print(', '.join([f"{key}: {record[key]}" for key in info]))

or even:

for key in info:
    print(key, ":", record[key], end=', ')
print()

Stop worrying about how many lines you use. Worry about writing readable, maintainable, and efficient code.

Iterate of the list of dictionaries. For each dictionary construct a line that is comprised of the values associated with the keys in info list.

competitorRankDetails = [
    {
        "phaseCode": "M1",
        "phaseName": "Moto 1",
        "phaseBlockCode": "M1",
        "rank": 1,
        "raceName": "46",
        "racePosition": 6,
        "result": 1,
        "score": 1,
        "hillTime": "2.754",
        "time": "30.755",
    },
    {
        "phaseCode": "M2",
        "phaseName": "Moto 2",
        "phaseBlockCode": "M2",
        "rank": 1,
        "raceName": "97",
        "racePosition": 7,
        "result": 1,
        "score": 2,
        "hillTime": "2.732",
        "time": "30.689",
    },
    {
        "phaseCode": "M3",
        "phaseName": "Moto 3",
        "phaseBlockCode": "M3",
        "rank": 1,
        "raceName": "148",
        "racePosition": 3,
        "result": 1,
        "score": 3,
        "hillTime": "2.745",
        "time": "30.692",
    },
]
info = ["phaseName", "racePosition", "hillTime"]

for d in competitorRankDetails:
    print(" , ".join(f"{i}: {d.get(i)}" for i in info))

Output:

phaseName: Moto 1 , racePosition: 6 , hillTime: 2.754
phaseName: Moto 2 , racePosition: 7 , hillTime: 2.732
phaseName: Moto 3 , racePosition: 3 , hillTime: 2.745

This gives the exact output you wanted. Explanation is given as comments inside the code

info = ['phaseName', 'racePosition', 'hillTime']   # add more keys if you want

# remove double quote after competitorRankDetails and replace ':' by '='
competitorRankDetails = [
        {
          "phaseCode": "M1",
          "phaseName": "Moto 1",
          "phaseBlockCode": "M1",
          "rank": 1,
          "raceName": "46",
          "racePosition": 6,
          "result": 1,
          "score": 1,
          "hillTime": "2.754",
          "time": "30.755"
        },
        {
          "phaseCode": "M2",
          "phaseName": "Moto 2",
          "phaseBlockCode": "M2",
          "rank": 1,
          "raceName": "97",
          "racePosition": 7,
          "result": 1,
          "score": 2,
          "hillTime": "2.732",
          "time": "30.689"
        },
        {
          "phaseCode": "M3",
          "phaseName": "Moto 3",
          "phaseBlockCode": "M3",
          "rank": 1,
          "raceName": "148",
          "racePosition": 3,
          "result": 1,
          "score": 3,
          "hillTime": "2.745",
          "time": "30.692"
        }
    ]

for y in competitorRankDetails: #Iterate thru competitorRankDetails

    value = '' # Intiate a string
    
    for x in info: # Iterate thru info
        
        # f-string as you want each 3 data in single line
        value = value + f'{x}: {y[x]}, '
        
    value = value[:-2] # to remove the comma and space at each line
    
    print(value)
    

Output:

phaseName: Moto 1, racePosition: 6, hillTime: 2.754
phaseName: Moto 2, racePosition: 7, hillTime: 2.732
phaseName: Moto 3, racePosition: 3, hillTime: 2.745
            
转载请注明原文地址:http://anycun.com/QandA/1746094441a91586.html