0% found this document useful (0 votes)
4 views2 pages

Python List Operations and Examples

Uploaded by

Shelmi ISAS
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)
4 views2 pages

Python List Operations and Examples

Uploaded by

Shelmi ISAS
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

1.

Write a program to print the smallest element in a list along with its index
my_list = [10, 3, 5, 2, 8, 2]
smallest = my_list[0]
index = 0
for i in range(1, len(my_list)):
if my_list[i] < smallest:
smallest = my_list[i]
index = i
print("The smallest element is", smallest, "at index", index)

2. Write a program to find the mean of a list


my_list = [10, 20, 30, 40, 50]
total = 0
for num in my_list:
total += num
mean = total / len(my_list)
print("The mean of the list is", mean)

3. Write a program to search for an element in a list


my_list = [10, 20, 30, 40, 50]
# Element to search
search_element = 30
found = False
for i in range(len(my_list)):
if my_list[i] == search_element:
print("Element", search_element, "found at index", i)
found = True
break
if not found:
print("Element", search_element, "not found in the list")

4. Write a program to count the frequency of given element in a list


my_list = [10, 20, 30, 20, 10, 20, 40]
element_to_count = 20
count = 0
for item in my_list:
if item == element_to_count:
count += 1
print("Element", element_to_count, "occurs", count, "times in the list")

5. Write a program to reverse an array of integers


my_list = [10, 20, 30, 40, 50]
reversed_list = []
for i in range(len(my_list) - 1, -1, -1):
reversed_list.append(my_list[i])
print("Reversed list:", reversed_list)

using built in function


1. Print the smallest element in a list along with its index
mylist = [10, 3, 5, 2, 8, 2]
smallest = min(mylist)
index = my_list.index(smallest)
print("The smallest element is", smallest, "at index", index)
2. Find the mean of a list
mylist = [10, 20, 30, 40, 50]
mean = sum(mylist) / len(my_list)
print("The mean of the list is", mean)

3. Search for an element in a list


mylist = [10, 20, 30, 40, 50]
searchelement = 30
if searchelement in mylist:
index = [Link](searchelement)
print("Element", searchelement, "found at index", index)
else:
print("Element", searchelement, "not found in the list")

4. Count the frequency of a given element in a list


mylist = [10, 20, 30, 20, 10, 20, 40]
elementtocount = 20
count = [Link](elementtocount)
print("Element", elementtocount, "occurs", count, "times in the list")

5. Reverse an array of integers


mylist = [10, 20, 30, 40, 50]
reversedlist = list(reversed(mylist))
print("Reversed list:", reversedlist)

You might also like