Collection Data-Types in python:
Collection:
collections are nothing but objects,
which represents group of objects.
Group of elements into a single object, every
element as a object.
Collections represents
group of elements into a single entity.
Python supports the following types of
Collections,they are
- list
- tuple
- set
- dict
Every Collection
type provides some methods to perform the
operations on the elements of
Collection Objects.
list:-
- list object can be created by using [ ] or by calling list function.
list1=[ ] or list1= list( )
- list object is a mutable object.
- The elements of the list can be mutable or immutable.
list=[ [10,20,30,40], 'siva', 3+4j, True, 3.2 ]
- Every element in the list is represented with unique index.
- list is supporting both +ve and -ve indexing.
- Insertion order is preserved.
input: x=[10,20,30,40]
print(x)
output: [10,20,30,40]
- Duplicate elements are allowed.
list1=[10,20,10,30,20,40,10]
- Heterogeneous elements are allowed.
list1=[ 2, 2.3, True, 3+4j, 'siva' ]
- list size is not fixed(it is a dynamic)
- in a list object every element as a object
note:
list is a sequence data type, because in list insertion order preserved, means x=[10,20,30,40]
print(x)
output: [10,20,30,40]
input and output follow the same order.
Example 1:
a=[ ]
print(a)
print(type(a))
print(len(a))
Output:
[ ]
<class 'list'>
0
Example 2:
b=list()
print(b)
print(type(b))
print(len(b))
Output:
[ ]
<class 'list'>
0
Example 3:
c=[ 11,22,33,44 ]
print(c)
print(type(c))
print(len(c))
Output:
[11, 22, 33, 44]
< class 'list' >
4
Example 4:
d=[ 100, 12.34, True, 'sivakrishna', 2+3j ]
print(d)
print(type(d))
print(len(d))
Output:
[ 100, 12.34, True, 'sivakrishna', (2+3j) ]
< class 'list' >
5
Example 5:
e=[100,200,100,300,200,100]
print(e)
print(type(e))
print(len(e))
Output:
[100, 200, 100, 300, 200, 100]
<class 'list'>
6
Example 6: retrieving the elements from list by using indexes
f=[100,200,100,300,200,100]
print(e)
print(e[3])
print(e[-2])
Output:
[100, 200, 100, 300, 200, 100]
300
200
Example 7: list slices
g=[100,200,100,300,200,100]
print(g)
print(e[2:5])
print(e[2:])
print(e[-3:-1])
Output:
[100, 200, 100, 300, 200, 100]
[100, 300, 200]
[100, 300, 200, 100]
[300, 200]
Example 8: modify/change the content in the list
h=[10,2,7,4]
print(h)
print(id(h))
h[2]="siva"
print(h)
print(id(h))
Output:
[10, 2, 7, 4]
55109640
[10, 2, 'siva', 4]
55109640
Example 9: retrieving the list element
l=[ 11, 12.23, True, 'siva', 4+6j ]
print(l)
print(type(l))
a,b,c,d,e=l
print(a,type(a))
print(b,type(b))
print(c,type(c))
print(d,type(d))
print(e,type(e))
Output:
[11, 12.23, True, 'siva', (4+6j)]
< class 'list' >
11 < class 'int' >
12.23 < class 'float' >
True < class 'bool' >
siva < class 'str' >
(4+6j) < class 'complex' >
Note:
Python support nested list (list inside another list) mechanism.
Example:
x=[[10,20,30],[40,50,60],[70,80,90]]
print(x)
print(type(x))
print(x[0])
print(type(x[0]))
print(x[0][0])
print(type(x[0][0]))
Output:
[ [10, 20, 30], [40, 50, 60], [70, 80, 90] ]
< class 'list' >
[10, 20, 30]
< class 'list' >
10
< class 'int' >
List Methods:
Perform the some operations on list elements, python provides some predefined methods, they are
- list.append(elem) ---> adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
Example:
x=[10,40,20,50]
print(x)
print(id(x))
x.append(30)
print(x)
print(id(x))
Output:
[10, 40, 20, 50]
55788040
[10, 40, 20, 50, 30]
55788040
- list.insert(index, elem) ---> inserts the element at the given index, shifting elements to the right.
Example:
y=[10,40,20,50]
print(y)
print(id(y))
y.insert(2,60)
print(y)
print(id(y))
Output:
[10, 40, 20, 50]
19265480
[10, 40, 60, 20, 50]
19265480
- list.extend(list2) ---> adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
Example:
a=[1,2,3,4]
print(a)
print(id(a))
b=[5,6,7,8]
print(b)
print(id(b))
a.extend(b)
print(a)
print(id(a))
Output:
[1, 2, 3, 4]
55183752
[5, 6, 7, 8]
55104200
[1, 2, 3, 4, 5, 6, 7, 8]
55183752
- list.index(elem) ---> 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).
Example:
x=[10,40,20,50]
print(x)
print(x.index(20))
Output:
[10, 40, 20, 50]
2
- list.remove(elem) ---> searches for the first instance of the given element and removes it (throws ValueError if not present).
Example
c=[1,2,3,4]
print(c)
print(id(c))
c.remove(2)
print(c)
print(id(c))
Output:
[1, 2, 3, 4]
55101320
[1, 3, 4]
55101320
- list.sort() ---> sorts the list in place (does not return it). (The sorted() function shown below is preferred.)
Example:
e=[10,4,2,9,6,1]
print(e)
print(id(e))
e.sort()
print(e)
print(id(e))
Output:
[10, 4, 2, 9, 6, 1]
55105032
[1, 2, 4, 6, 9, 10]
55105032
- list.reverse() ---> reverses the list in place (does not return it)
Example
f=[5,2,8,1,6]
print(f)
print(id(f))
f.reverse()
print(f)
print(id(f))
Output:
[5, 2, 8, 1, 6]
55173000
[6, 1, 8, 2, 5]
55173000
- list.pop(index) ---> removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).
Example:
g=[1,2,3,4,5]
print(g)
print(id(g))
g.pop()
print(g)
print(id(g))
g.pop(2)
print(g)
print(id(g))
Output:
[1, 2, 3, 4, 5]
55183752
[1, 2, 3, 4]
55183752
[1, 2, 4]
55183752
No comments:
Post a Comment