\n
To access a range of items in a list, use the slicing operator :<\/code>. With this operator, you can specify where to start the slicing, end, and specify the step.<\/p>\n\n\n\nFor example, the expression list1[ start : stop : step]<\/code> returns the portion of the list from index start to index stop, at a step size step.<\/p>\n\n\n\nfor 1st list: Start from the 1st index with step value 2 so it will pick elements present at index 1, 3, 5, and so on<\/li> for 2nd list: Start from the 0th index with step value 2 so it will pick elements present at index 0, 2, 4, and so on<\/li><\/ul>\n\n\nlist1 = [3<\/span>, 6<\/span>, 9<\/span>, 12<\/span>, 15<\/span>, 18<\/span>, 21<\/span>]\nlist2 = [4<\/span>, 8<\/span>, 12<\/span>, 16<\/span>, 20<\/span>, 24<\/span>, 28<\/span>]\nres = list()\n\nodd_elements = list1[1<\/span>::2<\/span>]\nprint(\"Element at odd-index positions from list one\"<\/span>)\nprint(odd_elements)\n\neven_elements = list2[0<\/span>::2<\/span>]\nprint(\"Element at even-index positions from list two\"<\/span>)\nprint(even_elements)\n\nprint(\"Printing Final third list\"<\/span>)\nres.extend(odd_elements)\nres.extend(even_elements)\nprint(res)\n<\/code><\/span>Code language:<\/span> Python<\/span> (<\/span>python<\/span>)<\/span><\/small><\/pre><\/i><\/button><\/i> Run<\/button><\/div><\/div><\/details><\/div>\n\n\n\nExercise 2: Remove and add item in a list<\/h3>\n\n\n\n Write a program to remove the item present at index 4 and add it to the 2nd position and at the end of the list.<\/p>\n\n\n\n
Given<\/strong>:<\/p>\n\n\nlist1 = [54<\/span>, 44<\/span>, 27<\/span>, 79<\/span>, 91<\/span>, 41<\/span>]<\/code><\/span>Code language:<\/span> Python<\/span> (<\/span>python<\/span>)<\/span><\/small><\/pre>\n\n\nExpected Output<\/strong>:<\/p>\n\n\n\nList After removing element at index 4 [34, 54, 67, 89, 43, 94]\nList after Adding element at index 2 [34, 54, 11, 67, 89, 43, 94]\nList after Adding element at last [34, 54, 11, 67, 89, 43, 94, 11]<\/pre>\n\n\n\nShow Hint<\/summary>\n
Use the list methods, pop()<\/code>, insert()<\/code> and append()<\/code><\/p>\n<\/div><\/details><\/div>\n\n\n\nShow Solution<\/summary>\n
pop(index)<\/code>: Removes and returns the item at the given index from the list.<\/li>insert(index, item)<\/code>: Add the item at the specified position(index) in the list<\/li>append(item)<\/code>: Add item at the end of the list.<\/li><\/ul>\n\n\nsample_list = [34<\/span>, 54<\/span>, 67<\/span>, 89<\/span>, 11<\/span>, 43<\/span>, 94<\/span>]\n\nprint(\"Original list \"<\/span>, sample_list)\nelement = sample_list.pop(4<\/span>)\nprint(\"List After removing element at index 4 \"<\/span>, sample_list)\n\nsample_list.insert(2<\/span>, element)\nprint(\"List after Adding element at index 2 \"<\/span>, sample_list)\n\nsample_list.append(element)\nprint(\"List after Adding element at last \"<\/span>, sample_list)<\/code><\/span>Code language:<\/span> Python<\/span> (<\/span>python<\/span>)<\/span><\/small><\/pre><\/i><\/button><\/i> Run<\/button><\/div><\/div><\/details><\/div>\n\n\n\nExercise 3: Slice list into 3 equal chunks and reverse each chunk<\/h3>\n\n\n\n Given<\/strong>:<\/p>\n\n\nsample_list = [11<\/span>, 45<\/span>, 8<\/span>, 23<\/span>, 14<\/span>, 12<\/span>, 78<\/span>, 45<\/span>, 89<\/span>]<\/code><\/span>Code language:<\/span> Python<\/span> (<\/span>python<\/span>)<\/span><\/small><\/pre>\n\n\nExpected Outcome<\/strong>:<\/p>\n\n\n\nChunk 1 [11, 45, 8]\nAfter reversing it [8, 45, 11]\nChunk 2 [23, 14, 12]\nAfter reversing it [12, 14, 23]\nChunk 3 [78, 45, 89]\nAfter reversing it [89, 45, 78]<\/pre>\n\n\n\nShow Hint<\/summary>\n
Divide the length of a list by 3 to get the each chunk size<\/li> Run loop three times and use the slice()<\/code> function to get the chunk and reverse it<\/li><\/ul>\n<\/div><\/details><\/div>\n\n\n\nShow Solution<\/summary>\n
Get the length of a list using a len()<\/code> function<\/li>Divide the length by 3 to get the chunk size<\/li> Run loop three times<\/li> In each iteration, get a chunk using a slice(start, end, step)<\/code> function and reverse it using the reversed()<\/code> function<\/li>In each iteration, start<\/code> and end<\/code> value will change<\/li><\/ul>\n\n\n