|
| 1 | +#Using python to manipulate lists |
| 2 | +''' |
| 3 | +A list in Python is used to store the sequence of various |
| 4 | +types of data. Python lists are mutable type. |
| 5 | +
|
| 6 | +Python knows a number of compound data types, |
| 7 | +used to group together other values. The most |
| 8 | +versatile of which is a list. |
| 9 | +Others include: |
| 10 | +tuple |
| 11 | +set |
| 12 | +dictionary |
| 13 | +Lists are written as a list of comma-separated |
| 14 | +values (items) between square brackets [] |
| 15 | +
|
| 16 | +Lists are mutable - this means that items can be changed |
| 17 | +
|
| 18 | +List have a bunch of methods available. |
| 19 | +append() Adds an element at the end of the list |
| 20 | +clear() Removes all the elements from the list |
| 21 | +copy() Returns a copy of the list |
| 22 | +count() Returns the number of elements with the specified value |
| 23 | +extend() Add the elements of a list (or any iterable), to the end of the |
| 24 | +current list |
| 25 | +index() Returns the index of the first element with the specified value |
| 26 | +insert() Adds an element at the specified position |
| 27 | +pop() Removes the element at the specified position |
| 28 | +remove() Removes the first item with the specified value |
| 29 | +reverse() Reverses the order of the list |
| 30 | +sort() Sorts the list |
| 31 | +''' |
| 32 | +#A list can be define as below |
| 33 | +L1 = ["John", 102, "USA"] |
| 34 | +L2 = [1, 2, 3, 4, 5, 6] |
| 35 | +print(type(L1)) |
| 36 | +print(type(L2)) |
| 37 | +''' |
| 38 | +If we try to print the type of L1, L2, and L3 using type() function |
| 39 | +then it will come out to be a "list". |
| 40 | +''' |
| 41 | + |
| 42 | +#lets check the list are same objects equal or not |
| 43 | +a = [1,2,"Peter",4.50,"Ricky",5,6] |
| 44 | +b = [1,2,"Peter",4.50,"Ricky",5,6] |
| 45 | +print(a is b) |
| 46 | +id(a) |
| 47 | +id(b) |
| 48 | +b = a |
| 49 | +print(a is b) |
| 50 | +id(a) |
| 51 | +id(b) |
| 52 | + |
| 53 | +##Let's check the first statement that lists are the ordered. |
| 54 | +a = [1,2,"Peter",4.50,"Ricky",5,6] |
| 55 | +b = [1,2,5,"Peter",4.50,"Ricky",6] |
| 56 | +a == b |
| 57 | +''' |
| 58 | +Both lists have consisted of the same elements, but the second list |
| 59 | +changed the index position of the 5th element that violates the order |
| 60 | +of lists. When compare both lists it returns the false. |
| 61 | +
|
| 62 | +Lists maintain the order of the element for the lifetime. That's why it |
| 63 | +is the ordered collection of objects. |
| 64 | +''' |
| 65 | +a = [1, 2,"Peter", 4.50,"Ricky",5, 6] |
| 66 | +b = [1, 2,"Peter", 4.50,"Ricky",5, 6] |
| 67 | +a == b |
| 68 | + |
| 69 | +#The basics |
| 70 | +squares = [1, 4, 9, 16, 25] |
| 71 | +squares |
| 72 | +#Indexing |
| 73 | +''' |
| 74 | + +---+---+---+---+---+ |
| 75 | + | 1 | 4 | 9 | 16 | 25| |
| 76 | + +---+---+---+---+---+ |
| 77 | + 0 1 2 3 4 |
| 78 | + -5 -4 -3 -2 -1 |
| 79 | +''' |
| 80 | + |
| 81 | +squares[0] # indexing returns the item 1 |
| 82 | +squares[-1] # 25 |
| 83 | +squares[-3:] # slicing returns a new list [9,16,25] |
| 84 | + |
| 85 | +#Create a list copy |
| 86 | +squares[:] # [1, 4, 9, 16, 25] |
| 87 | + |
| 88 | +#Concatenation (glue together) |
| 89 | +squares + [36, 49, 64, 81, 100] |
| 90 | +# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] |
| 91 | + |
| 92 | +#Alter items |
| 93 | +cubes = [1, 8, 27, 65, 125] # something's wrong here |
| 94 | +4 ** 3 # the cube of 4 is 64, not 65! |
| 95 | +cubes[3] = 64 # replace the wrong value |
| 96 | +cubes |
| 97 | + |
| 98 | +#list methods |
| 99 | +cubes.append(216) # add the cube of 6 |
| 100 | +cubes.append(7 ** 3) # and the cube of 7 |
| 101 | +cubes |
| 102 | + |
| 103 | +#Length |
| 104 | +letters = ['a', 'b', 'c', 'd'] |
| 105 | +len(letters) |
| 106 | + |
| 107 | + |
| 108 | +#Nesting |
| 109 | +a = ['a', 'b', 'c'] |
| 110 | +n = [1, 2, 3] |
| 111 | +x = [a, n] |
| 112 | +x = [ ['a', 'b', 'c'], [1, 2, 3] ] |
| 113 | +x |
| 114 | +x[0] |
| 115 | +x[0][1] |
| 116 | +x[1][2] |
| 117 | + |
| 118 | +#nesting 2nd |
| 119 | +a = ['a' , 'b' , 'c' , 'd'] |
| 120 | +b = [1,2,4,5] |
| 121 | +c = [1, 'abc' , 3 , 4] |
| 122 | +d = [a, b, c] |
| 123 | +d |
| 124 | +''' |
| 125 | +output [['a', 'b', 'c', 'd'], [1, 2, 4, 5], [1, 'abc', 3, 4]] |
| 126 | +''' |
| 127 | +d[0][3] #d |
| 128 | +d[2][1] #abc |
| 129 | +''' |
| 130 | +output 'd' |
| 131 | +''' |
| 132 | +d[1][2] |
| 133 | +''' |
| 134 | +output 4 |
| 135 | +''' |
| 136 | +d[3][1] |
| 137 | +''' |
| 138 | +Traceback (most recent call last): |
| 139 | +File "<stdin>", line 1, in <module> |
| 140 | +IndexError: list index out of range |
| 141 | +''' |
| 142 | +d[2][1] |
| 143 | +''' |
| 144 | +output 'abc' |
| 145 | +''' |
| 146 | + |
| 147 | +#Let's have a look at the list example in detail. |
| 148 | +student = ["Hamza", 21, "Spartan300"] |
| 149 | +Field1 = ["Wp full stack",1] |
| 150 | +Field2 = ["Mern Stack",2] |
| 151 | +HOD_Field1 = [10,"Sr. Farooq"] |
| 152 | +HOD_Field2 = [11, "Mr. Hamza"] |
| 153 | +print("printing Student data...") |
| 154 | +print("Name : %s, ID: %d, Team: %s"%(student[0],student[1],student[2])) |
| 155 | +print("printing Fields of Expert...") |
| 156 | +print("Field 1:\nName: %s, ID: %d\nField 2:\nName: %s, ID: %s"%(Field1[0],Field1[1],Field2[0],Field2[1])) |
| 157 | +print("HOD Details ....") |
| 158 | +print("HOD Name: %s, Id: %d"%(HOD_Field1[1],HOD_Field1[0])) |
| 159 | +print("HOD Name: %s, Id: %d"%(HOD_Field2[1],HOD_Field2[0])) |
| 160 | +print(type(student),type(Field1),type(Field2),type(HOD_Field1),type(HOD_Field2)) |
| 161 | + |
| 162 | +''' |
| 163 | +Output |
| 164 | +printing Student data... |
| 165 | +Name : Hamza, ID: 21, Team: Spartan300 |
| 166 | +printing Fields of Expert... |
| 167 | +Field 1: |
| 168 | +Name: Wp full stack, ID: 1 |
| 169 | +Field 2: |
| 170 | +Name: Mern Stack, ID: 2 |
| 171 | +HOD Details .... |
| 172 | +HOD Name: Sr. Farooq, Id: 10 |
| 173 | +HOD Name: Mr. Hamza, Id: 11 |
| 174 | +<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> |
| 175 | +''' |
| 176 | + |
| 177 | +#Updating List values |
| 178 | +''' |
| 179 | +Lists are the most versatile data structures in Python since they |
| 180 | +are mutable, and their values can be updated by using the slice |
| 181 | +and assignment operator. |
| 182 | +''' |
| 183 | +list = [1, 2, 3, 4, 5, 6] |
| 184 | +print(list) |
| 185 | +# It will assign value to the value to the second index |
| 186 | +list[2] = 10 |
| 187 | +print(list) |
| 188 | +# Adding multiple-element |
| 189 | +list[1:3] = [89, 78] |
| 190 | +print(list) |
| 191 | +# It will add value at the end of the list |
| 192 | +list[-1] = 25 |
| 193 | +print(list) |
| 194 | + |
| 195 | +''' |
| 196 | +Output |
| 197 | +[1, 2, 3, 4, 5, 6] |
| 198 | +[1, 2, 10, 4, 5, 6] |
| 199 | +[1, 89, 78, 4, 5, 6] |
| 200 | +[1, 89, 78, 4, 5, 25] |
| 201 | +''' |
| 202 | + |
| 203 | +#Python List Operations |
| 204 | +''' |
| 205 | +The concatenation (+) and repetition (*) operators work |
| 206 | +in the same way as they were working with the strings. |
| 207 | +''' |
| 208 | +l1 = [1, 2, 3, 4] |
| 209 | +l2 = [5, 6, 7, 8] |
| 210 | +print(l1*2+l2) |
| 211 | +print(l1+l2) |
| 212 | +print(len(l1)) |
| 213 | + |
| 214 | +#Iterating a List |
| 215 | +list = ["John", "David", "James", "Jonathan"] |
| 216 | +for i in list: |
| 217 | + print(i) |
| 218 | + |
| 219 | +#Adding elements to the list |
| 220 | +#Declaring the empty list |
| 221 | +l =[] |
| 222 | +#Number of elements will be entered by the user |
| 223 | +n = int(input("Enter the number of elements in the list:")) |
| 224 | +# for loop to take the input |
| 225 | +for i in range(0,n): |
| 226 | + # The input is taken from the user and added to the list as the item |
| 227 | + l.append(input("Enter the item:")) |
| 228 | +print("printing the list items..") |
| 229 | +# traversal loop to print the list items |
| 230 | +for i in l: |
| 231 | + print(i, end = " ") |
| 232 | + |
| 233 | +''' |
| 234 | +Output |
| 235 | +Enter the number of elements in the list:5 |
| 236 | +Enter the item:25 |
| 237 | +Enter the item:46 |
| 238 | +Enter the item:12 |
| 239 | +Enter the item:75 |
| 240 | +Enter the item:42 |
| 241 | +printing the list items |
| 242 | +25 46 12 75 42 |
| 243 | +''' |
| 244 | + |
| 245 | +#Removing elements from the list |
| 246 | +''' |
| 247 | +Python provides the remove() function which is used to remove the |
| 248 | +element from the list. Consider the following example to understand |
| 249 | +this concept. |
| 250 | +''' |
| 251 | +list = [0,1,2,3,4] |
| 252 | +print("printing original list: "); |
| 253 | +for i in list: |
| 254 | + print(i,end=" ") |
| 255 | +list.remove(2) |
| 256 | +print("\nprinting the list after the removal of first element...") |
| 257 | +for i in list: |
| 258 | + print(i,end=" ") |
| 259 | + |
| 260 | +''' |
| 261 | +Output |
| 262 | +printing original list: |
| 263 | +0 1 2 3 4 |
| 264 | +printing the list after the removal of first element... |
| 265 | +0 1 3 4 |
| 266 | +''' |
| 267 | + |
| 268 | +#List comprehension |
| 269 | +''' |
| 270 | +Python is known for helping us produce code that is elegant, simple to |
| 271 | +write, and reads almost as well as plain English. List comprehension |
| 272 | +is one of the language's most distinguishing features, allowing us to |
| 273 | +develop sophisticated functionality with just one line of code. On |
| 274 | +the other hand, many Python writers struggle to fully utilize the |
| 275 | +more complex aspects of list comprehension. Sometimes programmers |
| 276 | +may overuse them, resulting in much less efficient and |
| 277 | +difficult-to-read code. |
| 278 | +Using List Comprehension |
| 279 | +newlist = [expression for item in iterable if condition == True] |
| 280 | +''' |
| 281 | +#Example |
| 282 | +numbers = [3, 5, 1, 7, 3, 9] |
| 283 | +num = [] |
| 284 | +for n in numbers: |
| 285 | + num.append(n**2) |
| 286 | +print(num) |
| 287 | +#All of this can be accomplished with only single line of code using |
| 288 | +# list comprehension. |
| 289 | +#newlist = [expression for item in iterable if condition == True] |
| 290 | +numbers = [3, 5, 1, 7, 3, 9] |
| 291 | +num = [n**2 for n in numbers] |
| 292 | +print(num) |
0 commit comments