Contents

## list manipulation in python class 11 programs

Read list manipulation in python class 11 programs with sample run

### Q1. program to print elements of list[‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’] in separate lines along with element’s both indexes (positive and negative)

#### program :-

``````L=['q', 'w', 'e', 'r', 't', 'y']
length=len(L)
for a in range(length):
print("At indexes", a, "and",(a-length),"element :",L[a])``````

#### Sample run above program is :

1.At indexes 0 and -6 element : q
2. At indexes 1 and -5 element : w
3. At indexes 2 and -4 element : e
4. At indexes 3 and -3 element : r
5. At indexes 4 and -2 element : t
6. At indexes 5 and -1 element : y

### Q2. Program to find minimum element from a list of element along with its index in list.

#### program :-

``````lst = eval(input("Enter list :"))
length = len(lst)
min_ele = lst
min_index = 0
for i in range(1, length-1) :
if lst[i] < min_ele :
min_ele = lst[i]
min_index = i
print("Given list is :", lst)
print("The minimum element of the given list is:")
print(min_ele, "at index", min_index)
``````

#### Sample run of above program is :

Enter list : [2, 3, 4, -2, 6, -7, 8, 11, -9, 11]
Given list is : [2, 3, 4, -2, 6, -7, 8, 11, -9, 11]
The minimum element of fiven list is :
-9 at index 8

### Q3. Program to calculate mean of a given list of numbers.

#### program :-

``````lst = eval(input("Enter list :"))
length= len(lst)
mean = sum = 0
for i in range(0, length-1) :
sum += lst[i]
mean = sum/length
print("Given list is :", lst)
print("The mean of the given list is :", mean)
``````

#### Sample run of above program is :

Enter list : [7, 23, -11, 55, 13.5, 20.05, -5.5]
Given list is : [7, 23, -11, 55, 13.5, 20.05, -5.5]
The mean of the given list is : 15.364285714285714

### Q4. Program to search for an element in given list of numbers.

#### Program :-

``````lst = eval(input("Enter list :"))
length= len(lst)
element = int(input("Enter element to be searched for :"))
for i in range(0, length-1) :
if element == lst[i] :
print(element, "found at index", i)
break
else :                          #else of for loop
``````

#### Sample run of above program is :

Enter list : [2, 8, 9, 11, -55, -11, 22, 78, 67]
Enter element to be searched for : -11
-11 found at index 5

### Q5. Program to count frequency of a given element in a list of numbers.

#### program :-

``````lst = eval(input("Enter list :"))
length= len(lst)
element = int(input("Enter element :"))
count = 0
for i in range(0, length-1) :
if element == lst[i] :
count += 1
if count == 0 :
else :
print(element, "has frequency as", count, "in given list")
``````

#### Sample run of above program is :

Enter list : [1, 1, 1, 2, 2, 3, 4, 2, 2, 5, 5, 2, 2, 5]
Enter element : 2
2 has frequency as 6 in given list

### Q6. Program to find frequencies of all elements of a list. Also, print the list of unique elements in the list and duplicate elements in given list.

#### Program :-

``````lst = eval(input("Enter list :"))
length= len(lst)
uniq = [ ]                          # list to hold unique elements
dup1 = [ ]                         # list to hold duplicate elements
count = i = 0
while i < length :
element = lst[i]
count = 1                         # count as 1 for the element at lst[i]
if element not in uniq and element not in dup1 :
i += 1
for j in range(i, length) :
if element == lst[j] :
count += 1
else :
i += 1
print("Original list", lst)
print("Unique elements list", uniq)
print("Duplicates elements list", dup1)
``````

#### Sample run of above program is :

1. Enter list : [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
2. Element 2 frequency : 3
3. Element 3 frequency : 3
4. Element 4 frequency : 1
5. Element 5 frequency : 2
6. Element 6 frequency : 1
7. Element 7 frequency : 2
8. Element 1 frequency : 1
9. Element 9 frequency : 1
10. Original list [2, 3, 4, 5, 3, 6, 7, 3, 5, 2, 7, 1, 9, 2]
11. Unique elements list [4, 6, 1, 9]
12. Duplicates elements list [2, 3, 5, 7]

### Q7. Given list of integers, L, write code to add the integers and display the sum.

#### Program :

``````pos = 0                                        # start of list
sum = 0                                       # initial sum
while pos < len(L) :                    # loop through entire list
sum = sum + L[pos]           # add current item to sum
pos = pos + 1                      # move to next item in list
``````

### Q8. Write a program to find the second largest number of a list of numbers.

#### Program :-

``````lst = eval(input("Enter list :"))
length= len(lst)
biggest = secondbiggest = lst       # though logically not fair
for i in range(1, length) :
if lst[i] > biggest :
secondbiggest = biggest
biggest = lst[i]
elif lst[i] > secondbiggest :
secondbiggest = lst[i]
print("Largest number of the list :", biggest)
print("second Largest number of the list :", secondbiggest)
``````

### Q9. Write a program that inputs a list of numbers and shifts all the zeros to right and all non-zero numbers to left of the list.

#### Program :-

``````lst = eval(input("Enter list :"))
length= len(lst)
end = length-1
print("original list :", lst)
i = 0
while (i <= end) :
ele = lst[i]
if ele == 0 :
for j in range(i, end) :
lst[j] = lst[j+1]
else :
lst[end] = 0
end -= 1
if lst[i]! = 0 :                    # if after shifting, a consecutive 0 shifted 0 shifted to current place
i += 1
print("zero shifted : ", lst)
``````

### Q10. Extract two list-slices out of a given list of numbers. Display and print the sum of elements of first list-slice which contains every other element of the list between indexes 5 or 15. Program should also display the average of elements in second list slice that contains every fourth element of the list.

#### program :-

``````lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
slc1 = lst[5 : 15 : 2]
slc2 = lst[:: 4]
sum = avg = 0
print("slic1")
sum += a
print("slice1")
for a in slc1 :
sum += a
print(a, end = ' ')
print( )
print("sum of elements of slice1 :", sum)
print("slice2")
sum = 0
for a in slc2 :
sum += a
print(a, end = ' ')
print( )
avg = sum/len(slc2)
print("Average of elements of slice 2 :", avg)
``````

#### Sample run of above program is :

slice 1
6 8 10 12 14
sum of elements of slice 1 : 50
slice 2
1 5 9 13 17
Average of elements of slice 2 : 9

## for more :

### FAQ :-

#### Q 1. What is the difference between Python list and array?

ans. Lists and arrays are used in Python to store data(any data type- strings, integers etc.), both can be indexed and iterated also. … Arrays need to be declared whereas lists do not need declaration because they are a part of Python’s syntax. This is the reason lists are more often used than arrays.

#### Q 2. What are list comprehensions in Python?

ans.  List comprehensions are used for creating new lists from other iterables. As list comprehensions return lists, they consist of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

#### Q 3. How do you create a list?

ans. Create a new list

1. On your Android phone or tablet, open the Google Keep app .
2. Next to “Take a note,” tap New list .
3. Add a title and items to your list.
4. When you’re done, tap Back .

#### Q 4. What is list of list in python?

ans. ListLists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

#### Q 5. How do you create an empty list?

ans. This can be achieved by two ways i.e. either by using square brackets[] or using the list() constructor. Lists in Python can be created by just placing the sequence inside the square brackets [] . To declare an empty list just assign a variable with square brackets.