f_harrison
f_harrison

Reputation: 51

Python : Printing lists side by side

I am wondering how to print two lists of the same size next to each other.

I have got these lists from a nested dict.

Here are the lists:

[
  [
    "Chelsea", 
    "Liverpool", 
    "ManCity", 
    "Arsenal", 
    "Spurs", 
    "ManU", 
    "Southampton", 
    "West Bromwich", 
    "Everton", 
    "Bournemouth", 
    "Stoke", 
    "Watford", 
    "West Ham", 
    "Middlesbrough", 
    "Foxes", 
    "Burnley", 
    "Crystal", 
    "Sunderland", 
    "Swans", 
    "Hull"
  ], 
  [
    43, 
    37, 
    36, 
    34, 
    33, 
    30, 
    24, 
    23, 
    23, 
    21, 
    21, 
    21, 
    19, 
    18, 
    17, 
    17, 
    15, 
    14, 
    12, 
    12
  ]
]

Essentially, I want the list to be:

Chelsea: 44 Liverpool:37

etc...

Here is my python code:

@app.route('/League Standing', methods=['GET','POST'])
def show_league():
    text = request.form['league']
    connection = httplib.HTTPConnection('api.football-data.org')
    headers = {'X-Auth-Token': 'key', 'X-Response-Control': 'minified'}
    connection.request('GET', '/v1/competitions/'+text+'/leagueTable', None, headers)
    response = json.loads(connection.getresponse().read().decode())
    teamnames = [r['team'] for r in response['standing']]
    points = [r['points'] for r in response['standing']]
    for teamname, point in zip(teamnames, points):
        print('{}: {}'.format(teamname, point))

I have tried formatting using %, but am not getting anything!

Upvotes: 0

Views: 1018

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

You may use zip here as:

#               v  unpack content of list
for a, b in zip(*my_list):
    print '{}: {}'.format(a, b)

where my_list is the list mentioned in the question. In order to map these entries in the form of dict you have to type-cast the ziped value to dict as:

dict(zip(*my_list))

which will return:

{'ManU': 30, 'Liverpool': 37, 'Burnley': 17, 'West Bromwich': 23, 'Middlesbrough': 18, 'Chelsea': 43, 'West Ham': 19, 'Hull': 12, 'Bournemouth': 21, 'Stoke': 21, 'Foxes': 17, 'Arsenal': 34, 'Southampton': 24, 'Watford': 21, 'Crystal': 15, 'Sunderland': 14, 'Everton': 23, 'Swans': 12, 'ManCity': 36, 'Spurs': 33}

Upvotes: 4

Related Questions