19.Difference between indexing and Slicing?
By using Index we can find the position of the element and apply some actions.
lst = [1,2,3,4,5]
print(lst[3])
By using slicing we can select a particular range of elements in a list and apply some actions.
lst = [1,2,3,4,5]
print(lst[1:4])
20.Difference between sort and sorted?
Sort () method only supported in list, when we use this method it change the original sequence.
a = [1,8,2,7,3,6,9,4,5]
a.sort()
print(a)
Sorted () method supported in list, tuple, set, dictionary, when we use this method it create a newly sorted list. the original sequence are not change.
a = [1,8,2,7,3,6,9,4,5]
print(sorted(a))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a)
[1, 8, 2, 7, 3, 6, 9, 4, 5]
21.Difference between reverse and reversed?
reverse() method reverses the elements in the list and it change the original sequence it does not return anything.
a = [1,8,2,7,3,6,9,4,5]
print(a.reverse())
a
reversed() method returns an iterable object in reverse order. it does not change original sequence.
a = [1,8,2,7,3,6,9,4,5]
print(list(reversed(a)))
print(a)
22.Difference between copy and deep copy?
A shallow copy creates a new object which stored in original sequence.
import copy
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = copy.copy(x)
print("Old list:", x)
print("New list:", y)
A deep copy creates a new object and then, recursively, inserts copies into it of the objects found in the original. Changes in old list don't affect new list.
x = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
y = copy.deepcopy(x)
print("Old list:", x)
print("New list:", y)
23.How to remove duplicate elements in the list?
Using Set we can remove duplicates.
duplicate = [2, 4, 10, 20, 5, 2, 20, 4]
print(list(set(duplicate)))
24.How to find an index of an element in the python list?
Use list. index() method we can find the index of an element, It provide first occurrence of an item in a list.
a = [1,8,2,7,3,6,9,4,5]
a.index(3)
25. How to find the occurrences of an element in the python list?
Using count() function we can find the number of occurrences of an elements in a list.
a = [1,8,2,7,3,2,6,9,2,4,5]
a.count(2)
26. How to insert an item at a given position?
Using insert() function we can insert an item in any position.
a = [1,8,2,7,3,6,9,4,5]
a.insert(3,'Five')
print(a)
27. How to check if an item is in the list?
Using in inbuilt method we can find if an element is in list or not. it return True then The element was in list otherwise false.
a = [1,8,2,7,3,6,9,4,5]
print(3 in a)
28. How to flatten a list in python?
data = [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]]
flat_list = []
for item in data:
flat_list += item
print(flat_list)
29. How to convert python list to other data structures like set, tuple, dictionary?
List to Tuple:
x = [2, 4, 10, 20, 5, 2, 20, 4]
tuple(x)
List to Set:
x = [2, 4, 10, 20, 5, 2, 20, 4]
set(x)
List to Dictionary:
lst = ['a', 1, 'b', 2, 'c', 3]
dic = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
print(dic)
30. How to apply a function to all items in the list?
Using Map function we can applya function to all item of list.
a_list = ["a", "b", "c"]
map_object = map(str.upper, a_list)
new_list = list(map_object)
print(new_list)
31. How to filter the elements based on a function in a python list?
Using filter () function we can filter the elements with apply some conditions.
mark = [70, 60, 80, 90, 50]
filtered = filter(lambda score: score >= 70, mark)
print(list(filtered))
32. How python lists are stored in memory?
List is stored as distinct block of memory bound together by pointers.