Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
7 like 6 dislike
10.2k views
in Python Programming by Goeduhub's Expert (2.2k points)
edited by

Assignment/Task 2

  1. Is a list mutable? 

  2. Does a list need to be homogeneous?

  3. What is the difference between a list and a tuple.

  4. How to find the number of elements in the list?
  5. How to check whether the list is empty or not?
  6. How to find the first and last element of the list?
  7. How to find the largest and lowest value in the list?
  8. How to access elements of the list?
  9. Remove elements in a list before a specific index

  10. Remove elements in a list between 2 indices

  11. Return every 2nd element in a list between 2 indices

  12. Get the first element from each nested list in a list

  13. How to modify elements of the list?
  14. How to concatenate two lists?
  15. How to add two lists element-wise in python?
  16. Difference between del and clear?
  17. Difference between remove and pop?
  18. Difference between append and extend?
  19. Difference between indexing and Slicing?
  20. Difference between sort and sorted?
  21. Difference between reverse and reversed?
  22. Difference between copy and deepcopy?
  23. How to remove duplicate elements in the list?
  24. How to find an index of an element in the python list?
  25. How to find the occurrences of an element in the python list?
  26. How to insert an item at a given position?
  27. How to check if an item is in the list?
  28. How to flatten a list in python?
  29. How to convert python list to other data structures like set, tuple, dictionary?
  30. How to apply a function to all items in the list?
  31. How to filter the elements based on a function in a python list?
  32. How python lists are stored in memory?

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

3 Answers

2 like 1 dislike
by (342 points)
selected by
 
Best answer

1. Is a list mutable?

 Yes, List is Mutable We can change the value of list after assigning a value.

lst = [1,2,3,4,5]

lst[1] = 'Two'

print(lst)

2.Does a list need to be homogeneous?

 No, The list is don't need to be homogeneous, List can contain heterogeneous like integer, float, string, tuple, dictionary.

lst = [1,2.0,'Three',(4),{'Five':5}]

print(lst)

3.What is the difference between a list and a tuple.

List is Mutable, while Tuple is Immutable, tuples are faster then list.

list example:

lst = [1,2,3,4,5]

lst[1] = 'Two'

print(lst)

tuple example:

tpl = (1,'Two',3.0,[4])

tpl[1] = 2

it throws a TypeError: 'tuple' object does not support item assignment

4.How to find the number of elements in the list?

 Using len() function we can find the number of elements in list.

lst = [1,2,3,4,5]

len(lst)

5.How to check whether the list is empty or not?

 if len(lst)==0: using this function we can check list is empty or not.

lst = [1,2,3,4,5]

len(lst)==0

It return False

lst = []

len(lst)==0

It return True

6.How to find the first and last element of the list?

Using Index method we can find the first and last element of a list

lst = [1,2,3,4,5]

To find First element : lst[0].

To find Last element : lst[-1].

7.How to find the largest and lowest value in the list?

Using min() and Max() inbuild function we can find the minimum and maximum value of a element in a list.

lst = [1,2,3,4,5]

print(min(lst))

print(max(lst))

8.How to access elements of the list?

Using Index value we can access the elements in list.

lst = [1,2,3,4,5]

print(lst[3])

print(lst[1:4])

9.Remove elements in a list before a specific index.

Using some inbuild functions like pop, remove and del we can remove the elements from the list.

lst = [1,2.0,'Three',(4),{'Five':5}]

lst.pop(3# Using Index Value

lst.remove('Three'# Using Name Of The Value

del lst[1:3# Using Index/ Index Range

10.Remove elements in a list between 2 indices

a = [1,2,3,4,5,6,7,8,9]

del a[::2]

print(a)

11.Return every 2nd element in a list between 2 indices

Use Slicing Method we can return every 2nd element in a list.

a = [1,2,3,4,5,6,7,8,9]

a[::2]

12.Get the first element from each nested list in a list

def ext(lst):

  return [item[0for item in lst]

lst1 = [[12], [345], [6789]]

print(ext(lst1))

13.How to modify elements of the list?

Using index value and assignment operator we can modify the elements in list.

a = [1,2,3,4,5,6,7,8,9]

a[1]= 'hi'

14.How to concatenate two lists?

using + operator we can concatenate two list.

a = [1,2,3,4,5]

b = [6,7,8,9]

print(a+b)

15.How to add two lists element-wise in python?

list1 = [123]

list2 = [456]

sum = []

for (a,b) in zip(list1,list2):

  sum.append(a+b)

print(sum)

16.Difference between del and clear?

 To remove items by index or slice we can use the del method in python. =>  del list[index] or del list

lst = [1,2.0,'Three',(4),{'Five':5}]

del lst[1:3]

clear() method in python is used to empty the entire list. => list.clear()

lst = [1,2.0,'Three',(4),{'Five':5}]

lst.clear()

17.Difference between remove and pop?

remove() method removes the elements from list by parameter.  list.remove(parameter)

lst = [1,2.0,'Three',(4),{'Five':5}]

lst.remove('Three')

pop() method removes the elements from list by index value.    list.pop(index)

lst = [1,2.0,'Three',(4),{'Five':5}]

lst.pop(3)

18.Difference between append and extend?

append() method adds an element to a list. 

lst = [1,2,3,4,5]

lst1 = [1,2.0,'Three',(4),{'Five':5}]

lst.append(lst1)

lst

[1, 2, 3, 4, 5, [1, 2.0, 'Three', 4, {'Five': 5}]]

extend() method concatenates the first list with another list . 

lst = [1,2,3,4,5]

lst1 = [1,2.0,'Three',(4),{'Five':5}]

lst.extend(lst1)

lst

[1, 2, 3, 4, 5, 1, 2.0, 'Three', 4, {'Five': 5}]

0 like 0 dislike
by (278 points)

GO_STP_6266

https://www.linkedin.com/posts/sahil-parmar-4099391bb_google-colaboratory-activity-6803384374319566848-gOep

0 like 0 dislike
by (342 points)

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.

3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...