Lists
List :
=====
List is similar to array.
Array : similar datatype
List : Different datatypes
List are represented by []
List slicing
[start:stop:stepsize]
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print(list) # Prints complete list
print(list[0]) # Prints first element of the list
print(list[1:3]) # Prints elements starting from 2nd till 3rd
print(list[2:]) # Prints elements starting from 3rd element
print(tinylist * 2) # Prints list two times
print(list + tinylist) # Prints concatenated lists
This produce the following result −
['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']
Creating list using range() function :
----------------------------------------
range(start,stop,stepsize)
>>> lst = range(10)
>>> lst
range(0, 10)
>>> for i in lst:
... print(i)
...
0
1
2
3
4
5
6
7
8
9
>>> lst = range(5,10)
>>> for i in lst:
... print(i)
...
5
6
7
8
9
>>> i = 0
>>> while(i < len(lst)):
... print(lst[i])
... i += 1
...
5
6
7
8
9
>>>
>>> lst = range(10)
>>> type(lst)
<class 'range'>
>>> for i in lst:
... print(i)
...
0
1
2
3
4
5
6
7
8
9
>>> lst = list(range(10))
>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Updating the elements of list :
-------------------------------
>>> lst = list(range(10))
>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst = list(range(1,5))
>>> lst
[1, 2, 3, 4]
>>> lst.append(9)
>>> lst
[1, 2, 3, 4, 9]
>>> lst[1] = 8
>>> lst
[1, 8, 3, 4, 9]
>>> lst[1:3] = 10,11
>>> lst
[1, 10, 11, 4, 9]
>>> del lst[1]
>>> lst
[1, 11, 4, 9]
>>> lst
[1, 11, 4, 9]
>>> lst.reverse()
>>> lst
[9, 4, 11, 1]
>>> lst = [1,11,4,9]
>>> lst.remove(11)
>>> lst
[1, 4, 9]
>>> lst.reverse()
>>> lst
[9, 4, 1]
>>>
1. Write a program to print elements of list in reverse order.
days = ['SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY']
i = len(days)-1
print(i)
while(i >= 0):
print(days[i])
i -= 1
# using negative index
j = -1
while(j>=-len(days)):
print(days[j])
j -= 1
Concatenation of lists
---------------------
>>> lst1 = [10,20,30]
>>> lst2 = [40,50,60]
>>> lst = lst1 + lst2
>>> print(lst)
[10, 20, 30, 40, 50, 60]
>>>
Repetetion of list :
--------------------
>>> print(lst)
[10, 20, 30, 40, 50, 60]
>>> print(lst*2)
[10, 20, 30, 40, 50, 60, 10, 20, 30, 40, 50, 60]
>>> print(lst)
[10, 20, 30, 40, 50, 60]
>>> a = 30
>>> print(a in lst)
True
>>>
>>> details = [101,'srinivas']
>>> rno,name = details
>>> rno
101
>>> name
'srinivas'
>>> a,b = 10,20
>>> a
10
>>> b
20
>>>
Aliasing and cloning lists:
---------------------------
Giving a new name to an existing list is called 'aliasing'. The new name is called 'alias name'
>>> x = [10,20,30,40]
>>> y = x
>>>
>>> id(x)
21150696
>>> id(y)
21150696
>>> x[1] = 50
>>> x
[10, 50, 30, 40]
>>> y
[10, 50, 30, 40]
If the programmer wants two independent lists, he should not go for aliasing. On other hand
he should use the cloing or copying.
>>> z = x[:] # x is cloned as z
>>> x
[10, 50, 30, 40]
>>> z
[10, 50, 30, 40]
>>> x [0] = 60
>>> x
[60, 50, 30, 40]
>>> z
[10, 50, 30, 40]
The same can be achieved by copying the elements of one list to another list using copy()
>>> k = x.copy()
>>> x
[60, 50, 30, 40]
>>> k
[60, 50, 30, 40]
>>> x[3] = 70
>>> x
[60, 50, 30, 70]
>>> k
[60, 50, 30, 40]
>>>
Methods to process Lists:
=========================
>>> num = [10,20,30,40,50]
>>> n = len(num)
>>> print(' no.of elements %d'%n)
no.of elements 5
>>> num.append(60)
>>> print('num after appending 60: ',num)
num after appending 60: [10, 20, 30, 40, 50, 60]
>>> num.insert(0,5)
>>> print('num after inserting 5 at 0th position: ', num)
num after inserting 5 at 0th position: [5, 10, 20, 30, 40, 50, 60]
>>> num1 = num.copy()
>>> print('newly created list num1 : ',num1)
newly created list num1 : [5, 10, 20, 30, 40, 50, 60]
>>> num.extend(num1)
>>> print('num after appending num1: ',num)
num after appending num1: [5, 10, 20, 30, 40, 50, 60, 5, 10, 20, 30, 40, 50, 60]
>>> n = num.count(50)
>>> print('no.of times 50 found in the list num : ', n)
no.of times 50 found in the list num : 2
>>> num.remove(50)
>>> print('num after removing 50 : ',num)
num after removing 50 : [5, 10, 20, 30, 40, 60, 5, 10, 20, 30, 40, 50, 60]
>>> num
[5, 10, 20, 30, 40, 60, 5, 10, 20, 30, 40, 50, 60]
>>> num.pop()
60
>>> print('num after removing ending element: ', num)
num after removing ending element: [5, 10, 20, 30, 40, 60, 5, 10, 20, 30, 40, 50]
>>> num.sort()
>>> print('num after sorting :', num)
num after sorting : [5, 5, 10, 10, 20, 20, 30, 30, 40, 40, 50, 60]
>>> num.reverse()
>>> print('num after reversing: ', num)
num after reversing: [60, 50, 40, 40, 30, 30, 20, 20, 10, 10, 5, 5]
>>> num.clear()
>>> print('num after removing all elements: ', num)
num after removing all elements: []
Finding biggest and smallest elements in a List :
--------------------------------------------------
>>> num
[10, 23, 43, 22, 25, 64, 39, 99]
>>> n1 = max(num)
>>> print(n1)
99
>>> n2 = min(num)
>>> print(n2)
10
>>>
Sorting the list of elements:
-----------------------------
>>> num
[10, 23, 43, 22, 25, 64, 39, 99]
>>> num.sort()
>>> num
[10, 22, 23, 25, 39, 43, 64, 99]
>>> num.sort(reverse= True)
>>> num
[99, 64, 43, 39, 25, 23, 22, 10]
Nested lists:
------------------
>>> a = [80,90]
>>> b = [30,40,50,a]
>>> b
[30, 40, 50, [80, 90]]
>>> b[3]
[80, 90]
>>> for i in b[3]:
... print(i)
...
80
90
Nested list with Matrices:
------------------------------
>>> mat = [[1,2,3],[4,5,6],[7,8,9]]
>>> for i in mat:
... print(i)
...
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
>>> for i in mat:
... for j in i:
... print(j, end=' ')
... print()
...
1 2 3
4 5 6
7 8 9
>>>
List Comprehensions:
------------------------
List compreshensions represent creation of new lists from an iterable object(like a list,set,tuple,dictionar or range)
that satisfy a given condition.
List compreshensions contain very compact code usually a single statement that performs the task.
>>> squeares=[]
>>> for i in range(1,11):
... squeares.append(i ** 2)
...
>>> squeares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>
1.
>>> squear = [ i**2 for i in range(1,11)]
>>> print(squear)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
2. create a list for even numbers between 1 to 20
>>> even_numbers = [ i for i in range(1,20) if(i%2==0)]
>>> even_numbers
[2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>
3. If we have two lists 'x' and 'y' and we want to add each element of 'x' with each element of 'y'. you can write for loop as
>>> x = [10,20,30]
>>> y = [1,2,3,4]
>>> lst = []
>>> for i in x:
... for j in y:
... lst.append(i+j)
...
>>> lst
[11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34]
>>>
>>> lst1 = [ i+j for i in x for j in y]
>>> lst1
[11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34]
>>>
4. Take a list of strings, create a list with first letters of each string.
>>> fruits = [ 'apple','banana','cherry' ]
>>> first_letter= [ w[0] for w in fruits ]
>>> first_letter
['a', 'b', 'c']
>>>
5. Let's take two lists 'num1' and 'num2' with some numbers as
num1 = [1,2,3,4,5]
num2 = [10,11,1,2]
create another list num3 with numbers present in num1 but not in 'num2'
>>> for i in num1:
... if i not in num2:
... num3.append(i)
...
>>> num3
[3, 4, 5]
>>>
>>> num4 = [i for i in num1 if i not in num2]
>>> num4
[3, 4, 5]
>>>
No comments:
Post a Comment