line = "101;johnny 'wave-boy' Jones;USA;8.32;Fish;21"

s = {}

(s['id'], s['name'], s['country'], s['average'], s['board'], s['age']) = line.split(";")
# above, why is "s" and the end of the key list surrounded by parenthesis?

print("ID:         " + s['id'])
print("Name:       " + s['name'])
print("Country     " + s['country'])
print("Average     " + s['average'])
print("Board type: " + s['board'])
print("Age:        " + s['age'])

The program is also not displaying the ID.

Recommended Answers

All 5 Replies

It does show ID for me:

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> line = "101;johnny 'wave-boy' Jones;USA;8.32;Fish;21"
>>>
>>> s = {}
>>>
>>> (s['id'], s['name'], s['country'], s['average'], s['board'], s['age']) = line.split(";")
>>> # above, why is "s" and the end of the key list surrounded by parenthesis?
...
>>> print("ID:         " + s['id'])
ID:         101
>>> print("Name:       " + s['name'])
Name:       johnny 'wave-boy' Jones
>>> print("Country     " + s['country'])
Country     USA
>>> print("Average     " + s['average'])
Average     8.32
>>> print("Board type: " + s['board'])
Board type: Fish
>>> print("Age:        " + s['age'])
Age:        21
>>>

Cleaner way:

from __future__ import print_function
line = "101;johnny 'wave-boy' Jones;USA;8.32;Fish;21"
s = {}
## tuple parenthesis are optional
s['id'], s['name'], s['country'], s['average'], s['board'], s['age'] = line.split(";")
print("ID:         " + s['id'])
print("Name:       " + s['name'])
print("Country     " + s['country'])
print("Average     " + s['average'])
print("Board type: " + s['board'])
print("Age:        " + s['age'])
ID:         101
Name:       johnny 'wave-boy' Jones
Country     USA
Average     8.32
Board type: Fish
Age:        21

It`s better if you can use a for loop for key:value in a dicitionary.
Than just 6 print statement.

Here some code you can look at,with som sorted output that i see you asked about in an other post.

import pprint

info ="101;johnny 'wave-boy' Jones;USA;8.32;Fish;21"
stat = 'id,name,country,average,board,age'

info = info.split(';')
stat = stat.split(',')

my_Dict = {}

k = 0
for item in stat:
    my_Dict[item] = info[k]
    k += 1
    
pprint.pprint(my_Dict)
  
print '*'*35    

#Sort by key
key_sort = sorted(my_Dict.items(), key=lambda (v,k): (v,k))
for info, stat in key_sort:
    print '%-10s  %10s' % (info, stat)
    
print '*'*35

#Sort by value
value_sort = sorted(my_Dict.items(), key=lambda (k,v): (v,k))
for info, stat in value_sort:
    print '%-10s  %10s' % (info, stat)


##Out
{'age': '21',
 'average': '8.32',
 'board': 'Fish',
 'country': 'USA',
 'id': '101',
 'name': "johnny 'wave-boy' Jones"}
***********************************
age                 21
average           8.32
board             Fish
country            USA
id                 101
name        johnny 'wave-boy' Jones
***********************************
id                 101
age                 21
average           8.32
board             Fish
country            USA
name        johnny 'wave-boy' Jones

The original code works fine with Python24 through Python31.
The parenthesis, as tonyjv pointed out, are optional.

Python 3.x version after run through 2to3 module.

import pprint

info ="101;johnny 'wave-boy' Jones;USA;8.32;Fish;21"
stat = 'id,name,country,average,board,age'

info = info.split(';')
stat = stat.split(',')

my_Dict = {}

k = 0
for item in stat:
    my_Dict[item] = info[k]
    k += 1
    
pprint.pprint(my_Dict)
  
print('*'*35)    

#Sort by key
key_sort = sorted(list(my_Dict.items()), key=lambda v_k: (v_k[0],v_k[1]))
for info, stat in key_sort:
    print('%-10s  %10s' % (info, stat))
    
print('*'*35)

#Sort by value
value_sort = sorted(list(my_Dict.items()), key=lambda k_v: (k_v[1],k_v[0]))
for info, stat in value_sort:
    print('%-10s  %10s' % (info, stat))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.