Python book of magic/04.Lists

Contents | Previous | Next


Basics of list

edit

List in python just look the same as ordinary lists you know.You have previously learned about variables which store single value. So what is special about lists. Lists allow you as a programmer to store multiple values inside a single variable. Instead of having to make many variables, you can get away with a single variable.

So what are building blocks of lists ? Lists are made up of values and index numbers(position of value). Values are those things stored in the list while index no are numetrical values starting from zero representing the position of values in the list.

Let look at Example 01

Countries=["USA", "South Africa", "India"]

This is a simple list called countries which contain names of three countries. Countries is the name of variable containing our list, USA, South Africa, India are our values stored inside the list. So were is index numbers, index numbers are invisible but what you should know is that first value starting from left of list is 0 and second value 1 so on. The square brackets are used to create list in python and the commas(,) between the values are used to separate the values.

Now I think you understand how list work in python, let try to manipulate them.

List manipulation

edit

Our list is useless if we can't change or retrieve information inside it. List manipulation usually involves replacing, adding, identifying, getting values and getting index numbers. Most of these commands are performed by buit-in functions such as print(). Functions are identified by the fact that they end with parentheses(). Let's say we want the value stored at index no1, look at example below.

Example 02

Countries=["USA", "South Africa", "India"]
Num= Countries[1]
print(Num)

Output

South Africa

To get values using index no's use list_name[index_no], this will ongly work if you know list_name and index_no. Our output from previous example is South Africa- 2019 Japan rugby world cup winners. Now let replace index no0 with Japan.

Example 03

Countries= ["USA", "South Africa", "India"]
Countries[0]="Japan"
print(countries)

Output

["Japan", "South Africa", "India"]

That was easy, you only had to change value using list_name[index_no]= value and value in index no1 was replaced with the new value.

Let explore more functions related to lists.

Example 04

 Anything = ['tree',12,4,'father',wall,0]
print(Anything)

num=Anything[1:4]
print(num)

Anything.append(15)
Anithing.append('john')
print(Anything)

Anything.insert(2,33)
print(Anything)

num=Anything.index(12)
print(num)

for i in Anything:
  print(i)

Output

['tree',12,4,'father',wall,0]
['tree',12,4,'father']
['tree',12,4,'father','wall',0,15,'john']
['tree',12,33,4,'father','wall',0,15,'john']
1
'tree'
12
33
4
'father'
'wall'
0
15
'john'

In the first line we have created list calle d Anything which stores strings and integers. Usually lists store almost all data types. Strings are characterised by quotation marks and integers are found free as numbers as they are.

Second line slices through the list from index nuumber0 to 3. Index no4 is excluded but index no0 is included. The values in those index are stored and printed on screen.

The append() function is used to add a values into the end of list(right-most side). The value passed to it is the value to be added to the end of that specified list.

insert() work the same as append() but this allow you to add value at certain index. The first parameter(values passed to function) is the index value while second parameter is the value to be placed at the end of list.

The index() function gets you the index no of that particular value passed to it.

The last lines have to do with for loop, necessary for scanning through variables, lists, ductionaries, tuples, e.t.c. The loop scans from first to the last value of the list storing each value inside i and printing the values of i until the loop stops.

Contents | Previous | Next