Data Structures in a language determine the level of flexibility of using the language. If a Language has efficient, inbuilt data structures then the effort of the programmer is reduced. He does not have to code everything from the scratch. Furthermore, if it has user friendly syntax, it also makes the code more readable.
Python accounts for both readability and efficiency. It provides many inbuilt data structure classes that are suitable for day to day programming. In next 4 chapters, we will look at the details of list
s,tuple
s,set
s and dictionary
s.
First, let's look at Zen of Python - Python Design Principles
In [1]:
import this
Python is designed according to this philosophy. Now we shall examine basic data structures which comes handy in our journey of Python.
List is a mutable collection of elements(may be of same or different types), which is indexed by a 0-based integer. Lists are so much like C
arrays. But the capability of Python lists called Slicing makes them more powerful.
Creating an empty list
x = [] # [] denotes a list type
# or
x = list()
Creating list with some initial elements
x = [2,3,0,'g']
In [1]:
x = [1,2,4,5]
In [2]:
x
Out[2]:
In [3]:
x[3]
Out[3]:
In [4]:
x[-2]
Out[4]:
One can extract a portion of a list, and modify the value of it. If x
is a list, it is achieved by a statement in the form of
x[start:stop:step]
It returns elements of x
from index start
to the index stop
(excluding stop
) in the steps of step
. These 3 arguments are not mandatory. If not specified start
is set to 0
, stop
is set to length of list and step
is set to 1
In [5]:
x = [1,2,5,6,7,0,3]
In [6]:
x[1:3] # Access from x[1] to x[2]
Out[6]:
In [7]:
x[2:5:2] # Access from x[2] to x[4] in the steps of 2
Out[7]:
In [8]:
x[1:3] = [6] # They can modify original list
In [9]:
x # Look at modified list, 6 is replaced twice
Out[9]:
In [25]:
x[::-1] # Access the array in reverse order
Out[25]:
In [10]:
x[:] # Returns copy of list x
Out[10]:
You have observed that slices return a list, which have the reference to original list. Hence modifying slice results the change in original array.
In [11]:
del x[2]
In [12]:
x
Out[12]:
In [13]:
x = [4,3,5,0,1]
y = [2,1,5,4,0]
In [14]:
x + y
Out[14]:
In [15]:
y * 2
Out[15]:
Unlike the Operators, operations performed on list can act directly on lists and may not return anything
Here are some of operations on list. They are member functions of class list
. If x
is a list,
x.append(elem)
- adds a single element to the end of the list. It does not return the new list, just modifies the original list x
.x.insert(index, elem)
- inserts the element at the given index, shifting elements to the right.x.extend(list2)
- adds the elements in list2
to the end of the list. Using +
or +=
on a list is similar to using extend()
.x.index(ele)
- searches for the given element from the start of the list and returns its index. Throws a ValueError
if the element does not appear (use in
to check without a ValueError).x.remove(elem)
- searches for the first instance of the given element and removes it (throws ValueError
if not present)x.sort()
- sorts the list in place (does not return it). (The sorted()
function is preferred.)x.reverse()
- reverses the list in place (does not return it)x.pop(index)
- removes and returns the element at the given index. Returns the rightmost element if index
is omitted (roughly the opposite of append()).
In [16]:
x = [0,3,7,2,1]
In [17]:
x.append(9)
x
Out[17]:
In [18]:
x.insert(4,4)
x
Out[18]:
In [19]:
x.extend([8,7,6])
x
Out[19]:
In [20]:
x.remove(6)
x
Out[20]:
In [21]:
x.sort()
x
Out[21]:
In [22]:
x.reverse()
x
Out[22]:
In [23]:
x.pop()
Out[23]:
In [24]:
x.pop(0)
Out[24]:
In [25]:
x
Out[25]:
In [26]:
sorted(x)
Out[26]:
List elements can also be lists, which gives 2-D array like structure
In [27]:
x = [[2,3,4],
[1,2,2],
[2,3,4]]
In [28]:
x[1]
Out[28]:
In [29]:
x[2][1]
Out[29]:
In [30]:
x = [1,2,3]
len(x)
Out[30]:
In [31]:
x = [[2,3,4],2,['a','v']]
In [32]:
len(x)
Out[32]:
In [33]:
x = [1,2,3,0,5,4]
4 in x
Out[33]:
In [34]:
10 in x
Out[34]:
In [60]:
list(range(10))
Out[60]: