Associate Professor, CSE Containers A Container is an entity which contains multiple data items It is also known as a collection or a compound data type Lists, Sets, Tuples and Dictionaries are container data types
The two types of basic data structures / containers in
Python are Sequences and Collections Sequences and Collections Sequence is a group of items with a deterministic ordering. The order in which we put them in is the order in which we get an item out from them. Collection does not have a deterministic ordering. The ordering is arbitrary, but physically, they do have an order. There are six built-in types of sequences – strings, lists, tuples, bytes, bytearray and range There are two types of collections – sets and dictionaries Lists List is a sequence of comma-separated values (items / elements) between square brackets [ ] Each element of a List is assigned a number - its position or index. The first index is zero. Individual elements can be accessed (like elements in a string) using indices. The index value starts from 0. List can shrink or grow during execution of a program Lists are also known as dynamic array Lists Lists are commonly used for handling variable length data Lists usually contain similar data types but they can contain dissimilar data types as well Items can be repeated in a List, duplicates can exist Empty List is also feasible Entire List can be printed using the name of the list Lists can also be sliced Looping in Lists We can iterate through the list using while or for loop colors = ['red', 'blue', 'pink', 'yellow', 'white'] i=0 while (i<len(colors)): print(colors[i]) i+=1 colors = ['red', 'blue', 'pink', 'yellow', 'white'] for a in colors: print(a) Basic List Operations Mutability – Lists are mutable / changeable and can be updated Concatenation – One list can be concatenated (appended) at the end of another Merging – Two lists can be merged to create a new list Conversion – A string / tuple / set can be converted to a list using the list ( ) conversion function Basic List Operations Aliasing – One list can be assigned to the other and both refer to the same list. Changing one changes the other. This is shallow copy. Cloning – Copying the contents of one list to another. The two lists refer to different lists though both contain the same values. Changing one list does not change another. This is deep copy. Searching – An element can be searched using the in membership operator List Built-in Functions len (lst) Gives the total length or number of elements max (lst) Returns the maximum element from the list min (lst) Returns the minimum element from the list sum (lst) Returns the sum of elements from the list any (lst) Returns True if element from the list is True all (lst) Returns True if all the elements are True del (lst) Deletes element, slice of entire list sorted (lst) Returns sorted list, list remains unchanged reversed (lst) Used for reversing a list Basic List Operations Identity – Whether two variables are referring to the same list can be checked using the is identity operator Comparison – It is possible to compare contents of two lists. Comparison is done item by item till there is a mismatch Emptiness – We can check if a list is empty using a not operator. List Methods list.append(obj) Appends object obj to list list.count(obj) Returns count of how many times obj occurs in list list.extend(seq) Appends the contents of seq to list list.index(obj) Returns the lowest index in list that obj appears list.insert(index, obj) Inserts object obj into list at offset index list.pop(obj=list[-1]) Removes and returns last object or obj from list list.remove(obj) Removes object obj from list list.reverse() Reverses objects of list in place list.sort([func]) Sorts objects of list, use compare func if given