Python List Programs
In Python programming language, a list is a versatile and most used data type, it is just like a dynamically sized array. A list is used to store multiple items (of the same type as well as mixed type) in a single variable. Lists are created using the square brackets []. List items are indexed, ordered, changeable, and may contain duplicate values.
This section contains solved Python programs on Lists (like, creating lists, retrieving data from the lists, change the existing values of list items, removing the list items, etc.), practice these list programs to enhance the Python programming skills working on multiple values stored in a single variable. These programs contain the solved code, explanation, and output.
To get details about different data types visit mictechblog.blogspot. com
Program to print the elements of a list:
python
Copy code
def print_list(lst):
for item in lst:
print(item)
my_list = [1, 2, 3, 4, 5]
print_list(my_list)
Program to find the sum of all elements in a list:
python
Copy code
def sum_list(lst):
sum = 0
for item in lst:
sum += item
return sum
my_list = [1, 2, 3, 4, 5]
print("Sum of all elements:", sum_list(my_list))
Program to find the largest element in a list:
python
Copy code
def find_max(lst):
max = lst[0]
for item in lst:
if item > max:
max = item
return max
my_list = [1, 2, 3, 4, 5]
print("Largest element:", find_max(my_list))
Program to sort a list in ascending order:
python
Copy code
def sort_list(lst):
for i in range(len(lst)):
for j in range(0, len(lst) - i - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
return lst
my_list = [5, 4, 3, 2, 1]
print("Sorted list:", sort_list(my_list))
Program to remove duplicates from a list:
python
Copy code
def remove_duplicates(lst):
new_list = []
for item in lst:
if item not in new_list:
new_list.append(item)
return new_list
my_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
print("List without duplicates:", remove_duplicates(my_list))
Program to reverse a list:
python
Copy code
def reverse_list(lst):
return lst[::-1]
my_list = [1, 2, 3, 4, 5]
print("Reversed list:", reverse_list(my_list))
Program to find the second largest element in a list:
python
Copy code
def second_largest(lst):
max1 = lst[0]
max2 = lst[1]
for item in lst:
if item > max1:
max2 = max1
max1 = item
elif item > max2 and item != max1:
max2 = item
return max2
my_list = [1, 2, 3, 4, 5]
print("Second largest element:", second_largest(my_list))
People also ask
What is list programs in Python?
How do I make a list program in Python?
How do I practice a list in Python?
What is list () used for in Python?
How do I create a list in code?
How we can create a list?
How do I print a list of names in Python?
How do I make a list array?
How do you declare a list type in Python?
Can Python input () be a list?
What is type () in Python?
How do you list text in Python?
How do you list A to Z in Python?
How do you list an array in Python


0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home