List Manipulation in Python
List Manipulation in Python
List Manipulation in Python
1. How are lists different from the strings when both are sequences ?
SOLUTION:-
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 its elements.
(ii) Strings store single type of elements – all characters while lists can store elements belonging to different types. Assume the following list definition in Python.
2. What are nested lists?
SOLUTION:-
When a list is contained in another list as a member-element, it is called nested list, eg.
3. What is the difference between append() and extend() functions?
SOLUTION:-
The append() can append only a single element to a list while extend() can append a list of elements to a list, eg
>>> val = [17, 24, 15, 30]
>>> val.extend([34, 27]) >>> val.append(44)
>>> val [17, 24, 15, 38, 34, 27, 44]
4. What is the difference between sort) and sorted() functions ?
SOLUTION :-
Both sort() and sorted() sort the list elements, but, the primary difference between the list sort() function and the sorted() function is that the sort() function will modify the list it is working on.
The sorted() function will create a new list containing a sorted version of the list it is given. Th sorted() function will not modify the list passed as a parameter. The sortf) works on a list whereas the sorted() can work on different types of iterable sequetion Also, the sorted() function always returns a list whereas the sort() function modifies the list in-place and has no return value.
List Manipulation in Python
5. Define list ?
Solution :- A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created.
A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].
6. What are the Characteristics of Lists
Solution :- The list has the following characteristics:
The lists are ordered.
The element of the list can access by index.
The lists are the mutable type.
The lists are mutable types.
A list can store the number of various elements.
7. Define Indexing and splitting ?
Solution :- The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].
The index starts from 0 and goes to length – 1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.
We can get the sub-list of the list using the following syntax.
list_varible(start:stop:step)
The start denotes the starting index position of the list.
The stop denotes the last index position of the list.
The step is used to skip the nth element within a start:stop
8. Write a note on Updating List values?
Solution :-
Lists are the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator. Python also provides append() and insert() methods, which can be used to add values to the list.
list = [1, 2, 3, 4, 5, 6]
print(list)
# It will assign value to the value to the second index
list[2] = 10
print(list)
# Adding multiple-element
list[1:3] = [89, 78]
print(list)
# It will add value at the end of the list
list[-1] = 25
print(list)
The list elements can also be deleted by using the del keyword. Python also provides us the remove() method if we do not know which element is to be deleted from the list.
example :-
list = [1, 2, 3, 4, 5, 6]
print(list)
# It will assign value to the value to second index
list[2] = 10
print(list)
# Adding multiple element
list[1:3] = [89, 78]
print(list)
# It will add value at the end of the list
list[-1] = 25
print(list)
Popular Videos
UX for Teams
Learn the basics and a bit beyond to improve your backend dev skills.
Designer
SEO & Instagram
Learn the basics and a bit beyond to improve your backend dev skills.
Designer