Python: Programming Language for Beginners - Learn In A Day! (2015)
Chapter 9. Tuples, Lists and Dictionaries
We have seen earlier that variables are used to store little information, but what if one needs to store a list of data that doesn’t change with time.
For example consider a phone book. You have a lot of contact names and their numbers saved in it which do not change with time. So, it is highly difficult to create such thing with the help of variables. So, the problem can be solved using lists, tuples and dictionaries. Lists can be defined as a set of values. Numbering of each list is done starting from zero. Values can be added at the end of lists and if we want we can remove a few as well. Example: A friend list.
Tuples are similar to lists, but their values can’t be changed. Once values are given they are to be used without changing them. Numbering starts from zero. Example: Names of the months.
Dictionaries, the meaning is depicted in the name itself - a dictionary. In dictionaries, we have a ‘directory’ of words, and each has a definition. In python, this word is termed as 'key', whereas the definition as 'value'. It is possible to remove, add, or modify values in dictionaries. Example: Telephone diary.
Tuples
Tuples can be created easily by just giving the tuple a name, after which it carries a list of values. For example months in the year:
Example - creating a tuple
months= ('January','February','March','April','May','June',\
'July','August','September','October','November',' December')
Note, the'\' thing at the end of the first line helps carrying the code into the next line. It makes longer lines more readable. Spaces after the comma are optional.
Python organizes these values in numbered indexes that start from 0, in the order which you entered.
Table - tuple indicies
|
Index |
Value |
|
0 |
January |
|
1 |
Feb |
|
2 |
Mar |
|
3 |
Apr |
|
4 |
May |
|
5 |
Jun |
|
6 |
Jul |
|
7 |
Aug |
|
8 |
Sep |
|
9 |
Oct |
|
10 |
Nov |
|
11 |
Dec |
Lists
Lists are very much same as tuples. Lists are 'mutable', so their values are changeable. Generally we use lists and not tuples since we are able to make any changes easily. For example there are four cats called Snappy, Kitty, Jessie and Chester.
Example -Creating a List
cats = ['Snappy', 'Kitty', 'Jessie', 'Chester']
The only difference you find is the use of square brackets and not parenthesis.
Adding a value to the list can be done using the ‘append()’ function.
Example-Adding items to the list
cats.append('Catherine')
Example - Using an append function
#add a new value to the end of a list:
list_name.append(value-to-add)
Example - Deleting an item
#Remove your 1st cat, Snappy.
del cats[1]
Dictionaries
Till now we have given numbers and retrieved the values but now it’s the other way around. Initially creating a dictionary is similar to creating a tuple or list. We have an e.g. below depicting three contacts in it:
Example - Creating a dictionary
#Make the phone book:
phonebook = {'Emily Everett':6784346, 'Peter Power':7658344, \
'Lewis Lame':1122345}
Example -Adding entries in to a dictionary
#Add the person'GingerMan'to the phonebook:
phonebook['Ginger Man'] = 123456
The above line says - the key is 'Ginger Man', and the value is 123456.
Example -Removing entries from the dictionary
del phonebook[' Emily Everett ']
The 'del' operator deletes the variable, function, entry in a list or dictionary.
All materials on the site are licensed Creative Commons Attribution-Sharealike 3.0 Unported CC BY-SA 3.0 & GNU Free Documentation License (GFDL)
If you are the copyright holder of any material contained on our site and intend to remove it, please contact our site administrator for approval.
© 2016-2026 All site design rights belong to S.Y.A.