Unit 3
Unit 3
>>>l1[0] = 11
>>> l1[x:x] = l2 # works like insert
>>>l1[0] = [11,22]
# sublist at 0th index
Updating lists…….
• Using methods • Using + and * operators
+ Concatenation/ Join operator
• l1.append(x) – used to add an - used to combine elements of two lists
item x in list l1 >>>l1=[1,2,3]
• l1.extend(i) – used to add >>>l2=[‘A’,’B’,’C’]
iterable i at end of list l1 >>>l3=l1+l2
>>>l3
• l1.insert(i,x) – inserts item x at
[1,2,3,’A’,’B’,’C’]
index i in l1
• Multiplication operator
- used to repeat the elements of a list
>>>l1 = [‘A’,’B’]
>>>l2=l1*2
>>>l2
[‘A’,’B’,’A’,’B’]
Deleting elements of list
• Using value • Using index
• l1.remove(x) – removes element x from • l1.pop(x) – returns and removes
list element at index x
• del l1[x] - removes element x from list
• l1.clear() – removes all elements from list • del l1 – deletes the list l1
• l1.pop() – returns and removes last
element of list
• Check if item exists
>>>mylist = [ “apple”, “orange”, “cherry”, “banana”]
>>>if “apple” in mylist:
print(“Yes”)
Yes
>>>numbers = [1,2,3,4,5,6,7,8,9,10]
>>>days = [“sun”,”mon”,”tue”,”wed”]
>>>mylist = [1,2,3,4,5]
list.append(mylist,10)
list.extend(mylist, x) #where, x is an iterable
>>>t2= (50,)
>>>t4 =(1, 2, 3, 23.8, ’a’, ‘abc’, [10,20,30], (11, 12), {1,2}) #nested tuple
>>>t6 = tuple(range(5))
Accessing elements of tuple
• Positive indexing
• Negative indexing
• Slicing
Updating tuples
• Once created, elements cannot be added to or removed from the tuple
• If tuple contains mutable element, list then only the list elements can be updated,
>>>t1 = (1,2,3,[4,5])
>>>t1[3][0] = 14
>>>t1[3].remove(5)
>>>t1[3].append(100)
• Deletion • Looping
del t1 – deletes the tuple t1 >>>for x in (1,2,3):
>>>del t1 print(x**2)
• len(t1)
• max(t1)
• min(t1)
• sum(t1)
• sorted(t1) – returns a list with tuple elements in ascending order
Sets
• A set is an unordered collection of elements of same or different data
types much like a set in Mathematics
• The order of elements is not maintained in sets, it means the elements may
not appear in the same order as they entered into the set. Elements can be
numeric, string or tuple only.
• Lists and sets cannot be used as elements in sets, i.e. nested sets not allowed
• Since, sets are unordered, the elements cannot be retrieved using indexing or
slicing operations.
• It also does not allow duplicate elements.
• The elements of a set are separated by , and placed inside curly braces {}
• For example:
set1 = {10, -20, 15.5, ‘Pune’}
Creating sets
>>>s1 = {1,2,3,4}
>>>s3 = {“abc”}
>>>s4 = set()
>>>s6 = set(range(5))
>>>s7 = {x for x in W} #Set comprehension, where W is an iterable (string, list, tuple, set, dictionary)
Accessing elements of sets
• Sets are unordered, no indexing. So individual values cannot be
accessed.
>>>d[3]
>>>d.get(3) # does not raise error in case of wrong key
>>>d[x] = y
- If key x is present in d then its value is replaced by y , else pair x:y is added to d
>>>d.update(d1)
- where, d1 is a dictionary and if d1 has same keys then pairs in d will be
replaced by those in d1
Accessing dictionary items
• Looping
>>>d = {1:1, 2:4, 3:9}
• To get the keys • To get the values
>>>for x in d.keys(): >>>for x in d.values():
print(x) print(x)
>>>for x in d: >>>for x in d:
print(x) print(d[x])