Python has built-in collections those are
List | Tuple |
Set | Dict |
Python List:
- List is Insert order is preserved
- List is Duplicate values allowed
- List is Heterogeneous (or) Different type value’s allowed
- List is Grow able ( we can add/remove/update in list)
- List is declared in square ([]) brackets
- List values/objects will access by using Index
- List is mutable.
- Empty List: To create empty list. L = []
- If we know elements already, We need to implement list. L = [10,20,30].
- Nested List: To create nested list.
- L = [10,20,[30,40,50]]
- print(L[2][1]) => 40
- Traverse of Elements:
- l = [1,2,3,4,5,6]
- Using For loop:
- for x in l:
- print(x) => 1,2,3,4,5,6
- Using While loop:
- i = 0
- while i < len(l):
- print(l[i])
- i =i+1
- to get list of length need use len()
- l = [1,2,3,4]
- print(len(l)) => 3
- Count is count the number of object is there same.
- l = [1,2,3,4,2,4]
- print(l.count(4)) => 2
- Finding the index of an item in a list
- l = [1,2,3,4]
- print(l.index(1)] => 0
- Append(): Append useful to add values at end position.
- l = []
- l.append[10]
- l.append[20]
- print(l) => [10,20]
- Insert(): Insert useful to add values at specified position.
- insert(index, value)
- l = [1,2,3,4]
- l.insert(1,200)
- print(l) => [1,200,3,4]
- Extend(): extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list.
- l = [1,2,3]
- l2 = [4,5,6]
- l.extend(l2) => print(l) => [1,2,3,4,5,6]
- l.extend(‘stack’) =>print(l) => [1,2,3,’s’,’t’,’a’,’c’,’k’]
- Remove(): Remove method will remove the element at specified position and this method wont return anything. if value not there Value error will raise.
- l = [1,2,3,4,5,6]
- l.remove(1) => print(l) = >[2,3,4,5,6]
- pop(): to remove and return last element or using index. if elemets not there in list Index error will raise.
- l = [1,2,3,4,5] => z = l.pop() => print(z) => [1,2,3,4]
- Reverse(): reverse method reverses the elements of the list.
- l = [1,8,3,0]
- l.reverse()
- print(l) = > [0,1,3,8]
- Sort(): For sort method we should use homogeneous object (same object). By using sort we can arrange the elements asc/desc order.
- l = [1,5,2,7]
- l.sort() => print(l) => [1,2,5,7] => ASC order
- l.sort(reverse = True) => [7,5,2,1] => DESC Order
- List Concatenation: we can perform the concatenate between two list’s. concatenate will use + operator.
- l1 = [1,2,3,4] and l2 = [5,6,7,8]
- c = l1+l2 => print(c) => [1,2,3,4,5,6,7,8]
- List Replication: we can perform the replication between two list’s. Replication will use * operator.
- l = [1,2,3]
- print(l*2) => [1,2,3,1,2,3]
- Clear(): to clear all elements in list.
- l = [1,2,3,4] => l.clear() => print(l) => []
- Membership operator in List:
- l = [1,2,3,4]
- print(100 in l) => False
- print(10 in l) => True
- print(20 not in l) => False
- print(200 not in l) => True
- List Comprehension: The syntax of List comprehension.
- [expression for x in sequence if condition]
- l = [1,2,3,4,5,6,7,8]
- l2 = [x for x in l if x%2==0]
- print(l2) => [2,4,6,8]
Python Tuple:
- Tuple represent parenthesis ().
- Tuple is Duplicate values allowed
- Tuple is Heterogeneous (or) Different type value’s allowed
- Tuple values/objects will access by using Index
- List is Immutable.
- We can write tuple without using parenthesis ().
t = 10,20,30
print(type(t)) => tuple
- How can represent single value in tuple: t = (“a”,)
- Create empty tuple: t = ()
- Convert list to tuple
- l = [1,2,3,4]
- t = tuple(l)
- print(t) => (1,2,3,4)
- Tuple Concatenation: we can perform the concatenate between two Tuple’s. concatenate will use + operator.
- t1 = (1,2,3,4)
- t2 = (10,20,30,40)
- t3 = t1 + t2
- print(t3) => (1,2,3,4,10,20,30,40)
- Tuple Replication: we can perform the replication between two Tuple’s. Replication will use * operator.
- t1 = (1,2,3)
- t2 = t1 * 2
- print(t2) => (1,2,3,1,2,3)
- to get tuple of length need use len()
- t = (1,2,3,4)
- print(len(t)) => 4
- Count is count the number of object is there same.
- t = (1,2,3,1)
- print(t.count(1)) => 1
- Finding the index of an item in a tuple.
- t = (1,2,3)
- t.index(1) => 0
- t.index(900) => value error
- Sorted(): By using sorted we can arrange the elements asc/desc order.
- t = (5,1,2,7)
- printsorted(t)) => (1,2,5,7)
- Find min and ma value in tuple
- t = (10,1,0,5,100)
- print(min(t)) => 0
- print(max(t)) => 100
- Tuple Packing:
- Grouping elements into single
- a = 10, b = 20, c = 30
- t = a,b,c
- print(t) => 10,20,30
- Tuple Unpack:
- t = (1,2,3)
- a,b,c = t
- print(a,b,c) => 1,2,3
Difference between List and Tuple:
List | Tuple |
Elements contain [] | Elements contain () |
[] its mandatory | () its not mandatory |
Comprehension is there | Comprehension not there |
List objects cant be used as keys for the dictionary | Tuple objects can be use as keys for the dictionary |
Python Set:
- Duplicate value’s not allowed.
- Order is not preserved.
- s = {2,6,3,9}
- print(s) => {9,2,3,6}
- Heterogeneous allowed.
- Set is mutable.
- Set doesn’t support index.
- s = {0,1,5,2,4}
- print(s[0]) => set doesn’t support index.
- Create empty set.
- s = set()
- print(s)
- convert list, tuple, string to set
- t = (10,20,30,40) => tuple
- y = set(s)
- print(y) => {10,20,30,40}
- l = [10,20,30,40]
- m = set(l) => list
- print(m) => {10,20,30,40}
- s = “str” => string
- print(set(s)) => {“st”,”t”,”r”}
- add(): By using add() we can add only one item at a time.
- a=set()
- a.add(10)
- a.add(20)
- print(a) => {10,20}
- Update(): Here we can add able to add multiple elements at a time.
- s = {9,1,4,2}
- s2 = [8,0]
- s.update(s2)
- print(s) => {9,8,1,4,0,2}
- POP(): to remove random elements
- a = {1,5,2,7,3}
- a.pop()
- print(a) = >{1,5,2,3}
- Remove():to remove particular element.
- a = {1,5,2,7,3}
- print(a.remove(5))
- Set Comprehension:
- s = {x for x in range(1,6) if x%2=0}
- print(s) => {2,4}
Python Dictionary:
- Dictionary container key and value.
- Duplicate keys not allowed and value’s allowed.
- Heterogeneous allowed.
- d[key] = value
- d = {}
- d[‘sno’] = 100
- d[‘name’]= “stackorigin”
- print(d) => {‘sno’:100,”name”:”stackorigin”}
- Access elements in dictionary
- d = {‘sno’:100,”name”:”stackorigin”}
- print(d[‘sno’]) => 100
- Update dictionary value
- s = {‘sno’:100,”name”:”stackorigin”}
- s[‘sno’]= 1
- print(s) => {‘sno’:1,”name”:”stackorigin”}
- Delete dictionary key
- s = {‘sno’:100,”name”:”stackorigin”}
- del s[‘sno’]
- Get() get element in dictionary
- d = {‘sno’:100,”name”:”stackorigin”}
- print(d.get(‘sno’)) => 100
- If key not found error will raise
- so we need pass default value always
- s={‘sno’:100,”name”:”stackorigin”}
- print(s.get(“marks”), ‘sno’) => 100
- s={‘sno’:100,”name”:”stackorigin”}
- print(s.keys()) => keys will get
- print(s.values()) => values will get