Blog

List program in python - Bharosewale

Posted By Naveen Singh on 03 May 2023

python-program
list-program-in-python

 

1. Python program to interchange first and last elements in a list.

 

my_list = [2,5,8,4,3,6]

print(my_list)

temp = my_list[0]

my_list[0] = my_list[-1]

my_list[-1] = temp

print(my_list)

 

Output-:

[2, 5, 8, 4, 3, 6]

[6, 5, 8, 4, 3, 2]

 

2. Python program to count occurrences of an element  in a list

 

item = [2,3,4,4,5,7,8,8,8,9]

count=0

print("list is;",item)

x=int(input("Enter element do you want to check="))

for ele in item:

    if ele == x:

        count+=1

print(x,”present”,count,”times in a list”)

 

Output-:

[2,3,4,4,5,7,8,8,8,9]

Enter element do you want to check= 8

8 present 3 times in a list

 

3. Python program to find sum and average of list .

 

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

sum = 0

print(list)

for i in list1:

    sum = sum+i

print("sum of a list is:",sum)

avg = sum/len(list1)

print("average:",avg)

 

Output-:

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

sum of a list is : 55

average: 5.5

 

4.Python program to find length of a list.

 

my_list = [2,3,5,4,7,6,8]

print(list)

print(“length of a list is:”,len(my_list))

 

Output-:

[2,3,5,4,7,6,8]

length of a list is :7

 

5.Python program to reversing a list.

 

my_list = [2,3,5,4,7,6,8]

print(my_list)

my_list.reverse()

print(my_list)

 

Output-:

[2, 3, 5, 4, 7, 6, 8]

[8, 6, 7, 4, 5, 3, 2]

 

6. Python program to copying a list.

 

my_list = [2,3,5,4,7,6,8]

new_list = my_list.copy()

print(new_list)

 

Output-:

[2, 3, 5, 4, 7, 6, 8]

 

7. Python program to find largest number in a list.

 

my_list = [9,7,3,8,2,4]

my_list.sort()

print("largest element in a list is:",my_list[-1])

 

Output-:

largest element in a list is: 9

 

8.Python program to find second largest number in a list.

 

my_list = [9,7,3,8,2,2,4]

new_list = list(set(my_list))

new_list.sort()

print("second largest number in a list is:",new_list[-2])

 

Output-:

second largest number in a list is: 8

 

9. Python program to print even numbers in a list.

 

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

  for i in my_list:

     if(i%2==0):

        print(i,end=" ")

 

Output-:

2 4 6 8

 

10. Python program to find odd numbers in a list.

 

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

for i in my_list:

    if(i%2!=0):

        print(i,end=" ")

 

Output-:

3 5 7 9

 

11. Python program to count even and odd number in a list.

 

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

even_count = 0

odd_count = 0

for i in my_list:

    if(i%2==0):

        even_count += 1

    else:

        odd_count += 1

print("even numbers are:",even_count)

print("odd numbers are:",odd_count)

 

Output-:

even numbers are: 4

odd numbers are: 4

 

12. Python program to print positive numbers in a list.

 

my_list = [12,14,-3,-26,-18,7,29]

for i in my_list:

    if(i>0):

        print(i,end=" ")

 

Output-:

12 14 7 29

 

13. Python program to print negative numbers in a list.

my_list = [12,14,-3,-26,-18,7,29]

for i in my_list:

    if(i<0):

        print(i,end=" ")

 

Output-:

-3 -26 -18

 

14. Python program to count positive and negative numbers in a list.

 

my_list = [12,14,-3,-26,-18,7,29]

positive_count = 0

negative_count = 0

for i in my_list:

    if(i>0):

        positive_count += 1

    else:

        negative_count += 1

print(“positive numbers in a list:”,positive_count)

print(“negative numbers in a list:”,negative_count)

 

Output-:

positive numbers in a list: 4

negative numbers in a list: 3


 

Comments