Assignment/Task 2
1.Is a list mutable?
Yes!. It is Mutable, We can add elements or we can remove elements.
2. Does a list need to be homogeneous?
lists can either be homogeneous nor heterogeneous.
3.What is the difference between a list and a tuple?
Lists are mutable and tuple's are immutable. We cannot change the elements in the tuple while in the lists we can do change the inner elements.
4. How to find the number of elements in the list?
By using the built-in-function len(). We can find the totsl number of elements by using len() function.
5. How to check whether the list is empty or not?
By using the following code we will check the empty list,
list = []
if len(list):
print('The list is not empty')
else:
print('list is empty')
6. How to find the first and last element of the list?
list =[1,2,3,4,5,6]
element = [ list[0], list[-1] ]
print("The first and last element is "+ str(element))
7. How to find the largest and lowest value in the list?
list =[1,2,3,4,5,6]
element = [ list[0], list[-1] ]
print("The first and last element is "+ str(element))
8. How to access elements of the list?
We can access the list by using the index keyword. index()
9. Remove elements in a list before a specific index.
The pop() method is used to remove the elements in the list.
list =[1,2,3,4,5,6]
list.pop(2)
it removes the 3 vlaue from the list.
10. Remove elements in a list between 2 indices.
We can use the del() keyword here to remove the element between two index.
11. Return every 2nd element in a list between 2 indices
list= [1,2,3,4,5,6]
every_second_element= list[::2]
print(every_second_element)
12. Get the first element from each nested list in a list
new_list= [ [1,2,3,4,5], ['aswin', 'kumar'], ['orange', 'fruit'] ]
for item in new_list:
print((item[0]))
13. How to modify elements of the list?
List = ['aswin', 123, 'fruit']
List[0] = 'kumar'
print (List)
#the final output is
List = ['kumar', 123, 'fruit']
14. How to concatenate two lists?
list_1 = ['aswin', 'kumar']
list_2 = ['orange','apple']
new_list = [list_1 + list_2]
print(new_list)
15. How to add two lists element-wise in python?
list1 = [1, 2, 3]
list2 = [4, 5, 6]
sum_list = []
for (item1, item2) in zip(list1, list2):
sum_list. append(item1+item2)
print(sum_list)
16. Difference between del and clear?
del() keyword is used to delete a particular element. The clear( ) function is used to delete all the elements in a dictionary.
17. Difference between remove and pop?
pop() removes the element present at specified index, While remove() clear the deletes the matching element from the list
18. Difference between append and extend?
append() adds a single element to the end of the list whereas extend() can add multiple individual elements to the end of the list.
19. Difference between indexing and Slicing?
“Indexing” means referring to an element, “Slicing” means getting a subset of elements.
20. Difference between sort and sorted?
sort() function will modify the list and the sorted() function will create a new list containing a sorted version of the list.
21. Difference between reverse and reversed?
reverse() do the reverses the elements. reversed() doesn't actually reverse anything
22. Difference between copy and deepcopy?
A deep copy constructs a new compound object. The copy() method in Python returns a copy of the Set.
23. How to remove duplicate elements in the list?
LIST = [1,3,5,6,2,3,9,1,5,7]
new_list = []
for i in LIST:
for i not in new_list:
new_list.append(i)
print(new_list)
24. How to find an index of an element in the python list?
By using the index() keyword we can find the position of the element.
25. How to find the occurrences of an element in the python list?
def countX(list, x):
return list.count(x)
list = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print(''{} has occurred {} times''.format(x, countX(lst, x)))
26. How to insert an item at a given position?
insert() is an inbuilt function in Python that inserts a given element at a given index in a list.
27. How to check if an item is in the list?
my_list = [ 1, 6, 3, 5, 3, 4 ]
for i in my__list:
if(i == 4) :
print ("Element Exists")
28. How to flatten a list in python?
I'm Here using the list comprehension method
list = [[1, 2, 3, 4], [5, 6, 7], [8, 9]]
flat_list = [item for sublist in list for item in sublist]
print('Original list', list)
print('Transformed list', flat_list)
29. How to convert python list to other data structures like set, tuple, dictionary?
We can use the data type of which we required of it by using it before of that accesing the list.
30. How to apply a function to all items in the list?
By using list comprehension,
def double(integer):
return integer*2
integer_list = [1, 2, 3]
output_list = [double(i) for i in integer_list]
print(output_list)
31. How to filter the elements based on a function in a python list?
The filter() function iterates over the elements of the list and applies the fn() function to each element. It returns an iterator for the elements where the fn() returns True .
32. How python lists are stored in memory?
Python has the variable refer to the value. Similar to pointers in C, variables in Python refer to values. Python keeps an internal counter on how many references an object has. Instead of storing values in the memory space reserved by the variable