Simplifying 300 Python Programs on the list- Questions 71-80.

Simplifying 300 Python Programs on the list- Questions 71-80.

"Embark on a transformative journey as I dive deep into the realm of Python lists. Over the next 90 days, join me as I unravel the secrets of the list.

Q71-Write a Python program to insert an element at a specified position into a given list.

to insert an element at a specified position in the list, we have an insert method available in the list.

for example, the Original list:
[1, 1, 2, 3, 4, 4, 5, 1]
After inserting an element at the 2nd position in the said list:
[1, 1, 12, 2, 3, 4, 4, 5, 1]

ls=[1, 1, 2, 3, 4, 4, 5, 1]
ls.insert(2,12) #insert integer value -12 at 2nd index.
print(ls)

Result: [1, 1, 12, 2, 3, 4, 4, 5, 1]

Q72- Write a Python program to extract a given number of randomly selected elements from a given list.

for example, we need to select any three elements randomly from the given list.

Original list:
[1, 1, 2, 3, 4, 4, 5, 1]
Selected 3 random numbers of the above list:
[4, 4, 1]

for doing this, we have a random module, in this module, we have a sample() function.

import random
ls=[1, 1, 2, 3, 4, 4, 5, 1]
random_numbers=random.sample(ls,3) #select 3 randome numbers from the gien list
print(random_numbers)

Result: [5, 2, 4]

Q73- Write a Python program to generate combinations of n distinct objects taken from the elements of a given list.

for example:

Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Combinations of 2 distinct objects: [1, 2] [1, 3] [1, 4] [1, 5] .... [7, 8] [7, 9] [8, 9]

for doing this we can use the combinations() function from itertools module as below.

from itertools import combinations
help(combinations)
Help on class combinations in module itertools:

class combinations(builtins.object)
 |  combinations(iterable, r)
from itertools import combinations
ls=[1, 2, 3, 4, 5, 6, 7, 8, 9]
c=combinations(ls,2)
print(list(c))

Result: [(1, 2), (1, 3), (1, 4), (1, 5) ......]

Q74-Write a Python program to round every number in a given list of numbers and print the total sum multiplied by the length of the list.

for example:

Original list: [22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
Result:
243

ls=[22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
add=0
new_ls=[]
for i in ls:
  a=round(i) #take each value from the list and round off it.
  new_ls.append(a) #append all the values after rounding off to the new empty list.
for i in new_ls:
  add=add+i #adding all the values of new list.
len(ls)*add  #multiply by the lenth of the list.

OR

ls=[22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
add=0
for i in ls:
  a=round(i)
  add=add+a
result=len(ls)*add
print(result)

Q75-Write a Python program to round the numbers in a given list, print the minimum and maximum numbers, and multiply the numbers by 5. Print the unique numbers in ascending order separated by space.

for example:

Original list: [22.4, 4.0, 16.22, 9.1, 11.0, 12.22, 14.2, 5.2, 17.5]
Minimum value: 4
Maximum value: 22
Result:
20 25 45 55 60 70 80 90 110

ls=[22.4, 4.0, 16.22, 9.1, 11.0, 12.22, 14.2, 5.2, 17.5]
new_ls=[round(i) for i in ls] 
maximum=max(new_ls)
print(f"maximum numbe after rounding off the list is: {maximum}")
minumum=min(new_ls)
print(f"minimum number after rounding off the list is: {minumum}")
s=set(new_ls) #deleting duplicates
ls2=[i*5 for i in new_ls] #multiply each item by 5.
sort=sorted(ls2) #sorting the element of the list.
print(sort)

Q76-Write a Python program to create a multidimensional list (lists of lists) with zeros.

for example, we need to create a list of zeros.

Multidimensional list: [[0, 0], [0, 0], [0, 0]]

new_ls=[]
for i in range(3):
  new_ls.append([]) #appending three blank lists in the new_ls
  for j in range(2): 
    new_ls[i].append(0) #in each black list append 2 zeros.
print(new_ls)

OR

new_ls=[[ 0 for j in range(2)] for i in range(3)]
print(new_ls)

Result: [[0, 0], [0, 0], [0, 0]]

I know some people will not get the above code, let's simplify this

the above method is a list comprehension method by which we can minimize the code being used for iterating the loop.

break the above code into two parts:

first part: any loop we can write on the right side of the list and in the left side we need to put the operation that we need to by that loop

so first we need three empty lists to be appended in the new_ls list.

we can write this as below.

new_ls=[[]                      for i in range(3)]

#here new_ls will be created and appened three blank list.
#result= [[],[],[]]

Second Part: in the second part we want to insert two zeros in each empty list, so write that code inside the empty list.

new_ls=[[0 for j in range(2)]               for i in range(3)]

Q77- Write a Python program to create a 3X3 grid with numbers.

for example:

3X3 grid with numbers: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

for doing we will use the same method as above.

new_ls=[[j for j in range(1,4)]            for i in range(3)]
print(new_ls)

Result: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

Q78-Write a Python program to count the number of lists in a given list of lists.

for example:

Original list:
[[1, 3], [5, 7], [9, 11], [13, 15, 17]]
The number of lists in said list of lists:
4
Original list:
[[2, 4], [[6, 8], [4, 5, 8]], [10, 12, 14]]
The number of lists in said list of lists:
3

ls=[[1, 3], [5, 7], [9, 11], [13, 15, 17],4]
count=0
for  i in ls:
  if isinstance(i,list):
    count=count+1
print(count)

Q79-Write a Python program to find a list with maximum and minimum lengths.

ls=[[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]]
max_len=max(ls, key=len)
print(f"list of maximum lenth is: {max_len}")
print(f"length of that list is : {len(max_len)}")
min_len=min(ls,key=len)
print(f"list of minimum lenth is: {min_len}")
print(f"length of that list is : {len(min_len)}")