Computational Thinking and Programming-1
CHAPTER 11: LIST MANIPULATION
I. ANSWER THE FOLLOWING IN ONE OR TWO SENTENCES :
1. What is a list?
Lists are mutable sequences of python. i.e. you can change elements of a list in
place.
Lists can contain values of mixed data types.
Lists are formed by placing a comma – separated – list of expressions in square
brackets.
2. How are lists different from the strings when both are sequences?
The lists and strings are different in following ways:
i. The lists are mutable sequences while strings are immutable.
ii. In consecutive locations, strings store the individual characters while list
stores the references of the elements.
iii. Strings store single type of elements – all characters while lists can store
elements belonging to different types.
3. Discuss the utility and significance of Lists, briefly
List is a data structure in Python that is a mutable, or changeable, ordered
sequence of elements. Each element or value that is inside of a list is called an item. Just
as strings are defined as characters between quotes, lists are defined by having values
between square brackets [ ] .
Lists provide fast access to its elements using index numbers. Python lists are
mutable which makes them memory efficient. They serve as the basic building blocks
for programs that process large amounts of data.
4. What do you understand by mutability? What does “in place” memory updation
mean?
Mutable is when something is changeable or has the ability to change. In Python,
'mutable' is the ability of objects to change their values. These are often the objects that
store a collection of data.
There is no need to create another copy of the object in a new memory location with
the updated values. This updation of the existing memory location of the object is called
as in place memory updation.
5. Start with the list [8,9,10]. Do the following using list functions:
a. Set the second entry (index 1) to 17 b. Add 4, 5 and 6 to the end of the list
c. Remove the first entry from the list d. sort the list
e. double the list f. insert 25 at index 3
a. >>>l1 = [8,9,10]
>>>l1[1]=17
>>>l1
>>>[8,17,10]
b. >>>l1.extend(4,5,6) l1= [8,17,10,4,5,6]
c. >>>l1.remove(8) or l1.pop(0) l1=[17,10,4,5,6] or >>>8 >>>l1=[17,10,4,5,6]
d. >>>l1.sort( ) l1=[4,5,6,10,17]
e. >>>l1 = l1*2 l1=[4,5,6,10,17,4,5,6,10,17]
f. >>>l1.insert(3,25) l1=[4,5,6,25]
1
Computational Thinking and Programming-1
6. If a is [1,2,3]
a. What is the difference (if any) between a*3 and [a,a,a]
b. is a*3 equivalent to a+a+a?
c. What is the meaning of a[1:1]=9?
d. What’s the difference between a[1:2]=4 and a[1:1]=4?
a. a * 3 repeats the elements of the list whereas [a, a, a] creates nested list.
b. Yes, both a * 3 and a + a + a will result in [1, 2, 3, 1, 2, 3, 1, 2, 3].
c. It is called slicing of list however it will not extract any element and returns a
blank list a[]
d. Both are examples of slicing list. The difference between these two is that
a[1:2] will return a list with 2nd element only as (2,) and a[1:1] will return blank
list like [ ].
7. What’s a[1:1] if a is a list of at least two elements? And what if the list is shorter?
a[x:y] returns a slice of the sequence from index x to y - 1. So, a[1 : 1] will return an
empty list irrespective of whether the list has two elements or less as a slice from
index 1 to index 0 is an invalid range.
8. How are the statements lst = lst+3 and lst += [3] different, Where lst is a list?
Explain.
The statement lst = lst + 3 will give error as + operator in Python requires that
both its operands should be of the same type but here one operand is list and other
is integer.
The statement lst += [3] will add 3 at the end of the lst as += when used with lists
requires the operand on the right side to be an iterable and it will add each element
of the iterable to the end of the list.
9. How are the statements lst += “xy” and lst = lst + “xy” different, where lst is a list?
Explain.
The statement lst = lst + "xy" will give error as + operator in Python requires
that both its operands should be of the same type but here one operand is list
and other is string.
The statement lst += "xy" will add 'x' and 'y' at the end of the lst as += when
used with lists requires the operand on the right side to be an iterable and it
will add each element of the iterable to the end of the list.
10. What’s the purpose of the del operator and pop( ) method? Try deleting a slice.
The remove method first matching element (which is passed an argument) from
the list the pop method removes an element at a given index and will also return the
removed item you can also use the del keyword in python to remove an element or
slice from a list.
11. What does each of the following expressions evaluate to? Suppose that L is the list
l=["These", ["are", "a", "few", "words"], "that", "we", "will", "use"]
a. l[1][0::2] b. "a" in l[1][0] c. l[:1]+l[1]
d. l[2::2] e. l[2][2] in l[1]
a. ['are', 'few'] b. True c. ['These', 'are', 'a', 'few', 'words']
d. ['that', 'will'] e. True
2
Computational Thinking and Programming-1
12. What are list slices? What for can you use them?
List slicing refers to accessing a specific portion or a subset of the list for some
operation while the orginal list remains unaffected.
The stop represents the last index up to which the list slicing will go on. Its
default value is (length(list)-1) or the index of last element in the list.
13. Differentiate remove( ) and pop( ) function.
remove( ) pop( )
Delete an element whose value is known, It can remove only single element, not
remove( ) function is used. list slices.
It doesn’t returns the deleted element. pop( ) returns the deleted element too.
remove() function removes the first The pop() function removes the last
occurrence of the specified element element or the element based on the
index given
Once removed, the removed list alone is It displays the indexed number
displayed element, confirm the element by
pressing enter then, it should be
removed.
14. Does the slice operator always produce a new list?
No, slicing returns a list which is inside the original list.
15. Compare lists with strings, how are they similar and how are they different?
a. The similarity between Lists and Strings in Python is that both are
sequences.
b. Both strings and lists have lengths: a string's length is the number of
characters in the string; a list's length is the number of items in the list.
c. Each character in a string as well as each item in a list has a position, also
called an index. In python, positions start at 0, so the "h" in the above
string is at position 0, and the "o" is at position 4 (note: one less than the
length of the string). In the list above, "zebra" is at position 2.
The differences between them are:
1. Lists are mutable but Strings are immutable.
2. elements of a list can be of different types whereas a String only contains
characters that are all of String type.
3. A string is a sequence of characters between single or double quotes. A list is a
sequence of items, where each item could be anything (an integer, a float, a
string, etc).
16. What do you understand by true copy of a list? How is it different from shallow
copy?
In case of true copy changes made to the original list will not reflect in the copied list
and vice versa. Incase of shallow copy of a list, the elements of the original list are
not copied to new memory locations.
17. An index out of bounds given with a list name causes error. But not with list slices.
Why?
When we use an index, we are accessing a constituent element of the list. If the
index is out of bounds there is no element to return from the given index hence Python
3
Computational Thinking and Programming-1
throws list index out of range error whereas list slicing always returns a subsequence
and empty subsequence is a valid sequence.
18. What is the difference between appending a list and extending a list.
append( ) adds a single element to the end of the list
extend( ) can add multiple individual elements to the end of the list.
19. Differentiate sort() and sorted( ) function.
sort() sorted()
sort() will sort the list in-place, mutating sorted() will return a new sorted list
its indexes and returning None leaving the original list unchanged
sort() is a method of the list class and can sorted() accepts any iterable, while list
only be used with lists. should be sorted( ) and stored in
another identifier.
20. List out the list methods and built – in functions: