Use the str() method (not recommended)

You can always make use of str() method which converts any number into a stirng, so basically str(10) means that 10 is not a number anymore but a string.

>>> list[0] + str(list[1])
'Booboo10'

Now if you want to add a space between these two, just concatenate the space like this:

>>> list[0] + ' ' + str(list[1])
'Booboo 10'

Use a dictionary instead of a list (recommended)

Using a dictionary is a more efficient and much more natural way to work with these data, because in this particular example it is like a key & value pair. So in other words if I want to retrieve a person’s number I will ask for the this person’s name.

Note that the data returned not in the same order as the data entered. This is an important aspect of dictionaries. We don’t store data in the dictionaries because we are interested in their order (eg an alphabetic order). Now, we are only interested in a list of keys and in a list of values. Order doesn’t matter and it doesn’t make any difference. As I told you before, we are only interested to enter a name and retrieve its value number. Let’s verify that the system works:

>>> dict['Hommer']
7

Also remember if we are interested in what the keys are, we can use the keys() function.

>>> dict.keys()
['Asterix', 'Hommer', 'Orestis', 'Booboo']

Likewise we can use the values() function to return a list of values:

>>> dict.values()
[9, 7, 5, 10]

Calculate the average (easy way!)

Having such a function that returns the values as a list it would be useful if you were trying to compute the average, because it would be really easy to do it in one single line of code:

POPULAR CATEGORY

Unixmen provide Linux Howtos, Tutorials, Tips & Tricks ,Opensource News. It cover most popular distros like Ubuntu, LinuxMint, Fedora, Centos. It is your Gate to the the world of Linux/Unix and Opensource in General.