In [15]:

for i in range(5):
    print(i)
0
1
2
3
4

working with Lists

In [50]:
nephew = ['Manoj', 'Manish', 'Mamta']
In [49]:
# Print list
nephew
Out[49]:
['Manoj Radhika', 'Manish Radhika', 'Mamta Radhika']
In [20]:
# First element in the list
nephew[0]
Out[20]:
'Manoj'
In [21]:
# Error if we search something beyond list
nephew[5]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-21-9f7aea0c974f> in <module>()
      1 # Error if we search something beyond list
----> 2 nephew[5]

IndexError: list index out of range
In [37]:
# Length of the list can be obtained by using 'len'
len(nephew)

# can also try
# print('The length of the list "nephew" is: '), len(nephew)
Out[37]:
3
In [40]:
# using a loop
for j in range(len(nephew)):
    print (nephew[j])
Manoj
Manish
Mamta
In [54]:
# Let's append the list using simple loop
for i in range(3):
    nephew[i] = nephew[i] + ' Radhika'  #did you notice space before Radhika? 
    # alternatively add space separately
    # nephew[i] = nephew[i] + ' ' + 'Radhika'
    
print(nephew)
['Manoj Radhika Radhika Radhika Radhika', 'Manish Radhika Radhika Radhika Radhika', 'Mamta Radhika Radhika Radhika Radhika']
In [78]:
# list can not only be of one type but can be mixed up as well
mix_it_up = [1, [2,3], 'Manoj', ['Sarita', 'Kajal', [4, 5]]]
In [79]:
# print list
mix_it_up
Out[79]:
[1, [2, 3], 'Manoj', ['Sarita', 'Kajal', [4, 5]]]
In [80]:
# Print index
print(mix_it_up[3])
print(mix_it_up[3][2])
print(mix_it_up[3][2][1])
# mix_it_up[3][2][1]
['Sarita', 'Kajal', [4, 5]]
[4, 5]
5
In [81]:
# to add single element to mixed arrray like list, use 'append'
mix_it_up.append('Manish Kumar')

mix_it_up
Out[81]:
[1, [2, 3], 'Manoj', ['Sarita', 'Kajal', [4, 5]], 'Manish Kumar']
In [82]:
# what happens if we append again
mix_it_up.append('Vishnu Mahesh')

mix_it_up
Out[82]:
[1,
 [2, 3],
 'Manoj',
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh']
In [83]:
# or if we try to inset more elements at once using append

mix_it_up.append('Rajshri Verma', 'Sarika Wadhwani')

# unfortunately, append() takes exactly one argument
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-ed8d513c4214> in <module>()
      1 # or if we try to inset more elements at once using append
      2 
----> 3 mix_it_up.append('Rajshri Verma', 'Sarika Wadhwani')
      4 
      5 # unfortunately, append() takes exactly one argument

TypeError: append() takes exactly one argument (2 given)
In [84]:
# or append via list

mix_it_up.append(['Rajshri Verma', 'Sarika Wadhwani'])

mix_it_up

#it works but added a list to the list
Out[84]:
[1,
 [2, 3],
 'Manoj',
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh',
 ['Rajshri Verma', 'Sarika Wadhwani']]
In [85]:
# So, what's the alternative
#### concatenate a list using extend
mix_it_up.extend(['Soumya', 'Jaya'])

mix_it_up

## there we go....
Out[85]:
[1,
 [2, 3],
 'Manoj',
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh',
 ['Rajshri Verma', 'Sarika Wadhwani'],
 'Soumya',
 'Jaya']
In [100]:
# alternatively we can do it by assigning it to other object while adding new list
newlist1 = mix_it_up + ['Donald', 'Mickey']

newlist1
Out[100]:
[1,
 [2, 3],
 'Manoj',
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh',
 ['Rajshri Verma', 'Sarika Wadhwani'],
 'Soumya',
 'Jaya',
 'Donald',
 'Mickey']
In [101]:
# we can also shift elements in the list by using insert() method
newlist1.insert(1, 'This is an inserted text')

newlist1
Out[101]:
[1,
 'This is an inserted text',
 [2, 3],
 'Manoj',
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh',
 ['Rajshri Verma', 'Sarika Wadhwani'],
 'Soumya',
 'Jaya',
 'Donald',
 'Mickey']
In [102]:
# we can delete the elements either by using del()
del newlist1[1]
newlist1
Out[102]:
[1,
 [2, 3],
 'Manoj',
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh',
 ['Rajshri Verma', 'Sarika Wadhwani'],
 'Soumya',
 'Jaya',
 'Donald',
 'Mickey']
In [103]:
# or by the value using remove()
newlist1.remove('Manoj')
newlist1
Out[103]:
[1,
 [2, 3],
 ['Sarita', 'Kajal', [4, 5]],
 'Manish Kumar',
 'Vishnu Mahesh',
 ['Rajshri Verma', 'Sarika Wadhwani'],
 'Soumya',
 'Jaya',
 'Donald',
 'Mickey']
In [105]:
# let's sort the elements in the list
newlist1.sort()
newlist1
Out[105]:
[1,
 [2, 3],
 ['Rajshri Verma', 'Sarika Wadhwani'],
 ['Sarita', 'Kajal', [4, 5]],
 'Donald',
 'Jaya',
 'Manish Kumar',
 'Mickey',
 'Soumya',
 'Vishnu Mahesh']

Working with slices

In [126]:
squares = [0,1,4,9,16,25,36,49,64,81,100]
In [109]:
# slicing first two elements from the lists
squares[0:2]
Out[109]:
[0, 1]
In [111]:
# say, sub elements of 6th throgh 9th position
squares[6:10]
Out[111]:
[36, 49, 64, 81]
In [114]:
# starting from first element to some, say 5th, element
squares[:6]
Out[114]:
[0, 1, 4, 9, 16, 25]
In [116]:
# starting from 5th until last
squares[4:]
Out[116]:
[16, 25, 36, 49, 64, 81, 100]
In [117]:
# element from the last.. last element is denoted by -1
squares[-1]
Out[117]:
100
In [123]:
#last 4
squares[-4:]
Out[123]:
[49, 64, 81, 100]
In [127]:
# add two more elements after last position
squares[11:12] = [121, 144]
squares
Out[127]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144]
In [130]:
# we can also delete last two elements using del
del squares[-2:]
In [131]:
print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [147]:
import math
for values in squares:
    print "Element: ", values, "  :: Square Root: ", math.sqrt(values)
Element:  0   :: Square Root:  0.0
Element:  1   :: Square Root:  1.0
Element:  4   :: Square Root:  2.0
Element:  9   :: Square Root:  3.0
Element:  16   :: Square Root:  4.0
Element:  25   :: Square Root:  5.0
Element:  36   :: Square Root:  6.0
Element:  49   :: Square Root:  7.0
Element:  64   :: Square Root:  8.0
Element:  81   :: Square Root:  9.0
Element:  100   :: Square Root:  10.0
In [152]:
# we can also print index and value
for index, values in enumerate(squares):
    print "Element ", index, ": -->", values 
Element  0 : --> 0
Element  1 : --> 1
Element  2 : --> 4
Element  3 : --> 9
Element  4 : --> 16
Element  5 : --> 25
Element  6 : --> 36
Element  7 : --> 49
Element  8 : --> 64
Element  9 : --> 81
Element  10 : --> 100
In [ ]:

 

Advertisements