Home > Software engineering >  extracting dictionaries from a list without quote mark [closed]
extracting dictionaries from a list without quote mark [closed]

Time:09-16

I have these dicts:

    Iran = {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}
Spain = {'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5}
my_team = [Iran, Spain]

after sort them like below

d = sorted(my_team, key=lambda y: (y['points'], y['wins']), reverse=True)

how can I print like below, without quote marks(' '):

Spain  wins:1 , loses:0 , draws:2 , goal difference:2 , points:5
Iran  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4

CodePudding user response:

lst = [{'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5},
       {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}]

for d in lst:
    print(','.join([f'{k}:{v}' for k, v in d.items()]))

Prints:

wins:1,loses:0,draws:2,goal difference:2,points:5
wins:1,loses:1,draws:1,goal difference:0,points:4

Edit2

Iran = {'country': 'Iran', 'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}
Spain = {'country': 'Spain', 'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5}
my_team = [Iran, Spain]
d = sorted(my_team, key=lambda y: (y['points'], y['wins']), reverse=True)

for item in d:
    print(item['country']   ': '   ', '.join([f'{k}:{v}' for k, v in item.items() if k != 'country']))

Prints:

Spain: wins:1, loses:0, draws:2, goal difference:2, points:5
Iran: wins:1, loses:1, draws:1, goal difference:0, points:4

CodePudding user response:

convert your dictionary to string then use replace() as the following:

var = [{'wins': 1, 'loses': 0, 'draws': 2, 'goal difference': 2, 'points': 5},
 {'wins': 1, 'loses': 1, 'draws': 1, 'goal difference': 0, 'points': 4}, ]
print(str(var).replace("'", ""))
  • Related