Slicing in Python
Slicing is a powerful concept in Python. Learn how to use slicing to achieve specific objectives. These flashcards provide code snippets that can be directly used in searching and sorting algorithms, such as binary search, insertion sort, etc.
14 flashcards · Programming Languages · by kgashok
A slice is an object containing a portion of a sequence.
This is another name for iterables in Python.
How are slices created?
A slice is created using the subscript notation, [] with colons between numbers when several are given, such as in variable_name[1:3:5]. The bracket (subscript) notation uses slice objects internally. Also see slice from the documentation. Also see slice in the glossary.
You have a list al = [1, 2, 3, 4, 5, 6] Write code that uses slicing to divide the list into two equal halves, lower and upper. Ref: MergeSort requires you to split a list in half, in the divide-and-conquer approach.
Alternatively, al = [1, 2, 3, 4, 5, 6] mid = len(al) // 2 lowerhalf = al[:mid] upperhalf = al[mid:] print(lowerhalf, upperhalf) >> [1, 2, 3], [4, 5, 6]
You have a list al = [1, 2, 3, 4, 5] Write code that slices the list into two halves. The lower half containing all elements upto the mid element but not including it. The upper half containing all elements except the mid element
al = [1, 2, 3, 4, 5] mid = len(al) // 2 lowerhalf = al[:mid] upper_without_mid = al[mid+1:] print(lowerhalf, upper_without_mid) >> [1, 2], [4, 5]
You have a list al = [1, 2, 4, 5, 6, 3] Write slicing code which will update the list by right shifting all elements starting from index 2 (i.e. element 4 and onwards) by exactly one position Resultant list required: [1, 2, 4, 4, 5, 6, 3]
al = [1, 2, 4, 5, 6, 3] al[2+1:] = al[2:] print(al) [1, 2, 4, 4, 5, 6, 3]
You have a list al = [1, 2, 4, 5, 6, 3] Write slicing code which will update the list by right shifting all elements starting from index 2 (i.e. element 4 and onwards) up to index 5 (but not including) by exactly one position The length of the list will need to remain the same at 6 Resultant list required: [1, 2, 4, 4, 5, 6]
al = [1, 2, 4, 5, 6, 3] al[2+1:5+1] = al[2:5] print(al) [1, 2, 4, 4, 5, 6] You can also write the slice code as al[3:6] = al[2:5]
You have a list al = [1, 2, 4, 5, 6, 3] Write code to move 3 to the correct index so that the list is sorted. Resultant list required: [1, 2, 3, 4, 5, 6]
al = [1, 2, 4, 5, 6, 3] al[2+1:5+1] = al[2:5] al[2] = 3 print(al) [1, 2, 3, 4, 5, 6]
There is a need to divide up a list progressively into smaller halves containing the first elements. This needs to continue until the list contains only one element. Write code snippet including slice code to perform this, and print the intermediary results as well. For e.g. al = [1, 2, 3, 4, 5, 6, 7, 8] al = [1, 2, 3, 4] al = [1, 2] al = [1]
There is a need to divide up a list progressively into smaller halves containing the first elements. This needs to continue until the list contains only one element. Write a recursive function code to perform this, and print the intermediary results as well. For e.g. al = [1, 2, 3, 4, 5, 6, 7, 8] al = [1, 2, 3, 4] al = [1, 2] al = [1]
A list of numbers alist is generated using the code list(range(10)). Write code to print the sum of all the numbers located in the odd indices of the list alist. Use constructs (like for-loops, list comprehension, etc.) other than slicing. Use of enumerate is also recommended
Enumeration of alist is [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)] The numbers in odd indices are [1, 3, 5, 7, 9] , the sum of which is 25
A list of numbers al is generated using the code list(range(10)). Write code to print the sum of all the numbers located in the odd indices of the list alist. Using slicing to arrive at a one-liner statement
The code is purposefully given in image form (to discourage cut-and-paste) to encourage you to type it out in the interpreter and see the results for yourself.
A list of numbers alist is generated using the code list(range(10)). Write code to print the sum of all the numbers located in the even indices of the list alist. Using slicing function to arrive at a two-line statement
Numbers present in the even indices can be summated and printed as follows: even_idx_nums = slice(None, None, 2) print(sum(alist[even_idx_nums])) >> 20
Write a code snippet to truncate a list by eliminating the first 2 elements of the list and the last 2 elements of the list. The truncation process must be carried out in the same list. For e.g. if al = [1, 2, 3, 4, 5] The resultant list must be [3]
By using relative indexing, the code is a one-liner again!
Can you visualize MergeSort?
http://j.mp/mergeVisual