KEMBAR78
List Programs With Outputs | PDF
0% found this document useful (0 votes)
2 views2 pages

List Programs With Outputs

Python lists

Uploaded by

zenitsu123my
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

List Programs With Outputs

Python lists

Uploaded by

zenitsu123my
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# 1) Add elements to a list and print the final list.

lst = [10, 20, 30, 40, 50]


# [10, 20, 30, 40, 50]

# 2) Find the sum and average of elements in a list.


total = sum(lst)
average = total / len(lst)
# Sum = 150, Average = 30.0

# 3) Find the maximum and minimum element in a list.


maximum = max(lst)
minimum = min(lst)
# Max = 50, Min = 10

# 4) Count the frequency of an element in a list.


freq = lst.count(20)
# Frequency of 20 = 1

# 5) Remove all duplicates from a list


dup_list = [10, 20, 20, 30, 10, 40]
no_dup = list(set(dup_list))
# [40, 10, 20, 30] # (order may vary)

# 6) Reverse a list without using reverse() method


reversed_list = lst[::-1]
# [50, 40, 30, 20, 10]

# 7) Find even and odd numbers separately from a list


even = [x for x in lst if x % 2 == 0]
odd = [x for x in lst if x % 2 != 0]
# Even = [10, 20, 30, 40, 50], Odd = []

# 8) Merge two lists and sort the result


list1 = [10, 30, 50]
list2 = [20, 40, 60]
merged = sorted(list1 + list2)
# [10, 20, 30, 40, 50, 60]
# 9) Check if a list is a palindrome
palindrome_list = [1, 2, 3, 2, 1]
is_palindrome = palindrome_list == palindrome_list[::-1]
# True

# 10) Find the second largest number in a list.


unique_sorted = sorted(set(lst))
second_largest = unique_sorted[-2]
# 40

You might also like