Program to get the median of list elements

In this program, we will get the median of all the elements from the list.

Steps to solve the program
  1. Take an input list with some values.
  2. Sort list items with the sorted() function.
  3. Get the length of the sorted list.
  4. If the length of the list is an even number then the median of the list will average two mid-values of the list.
  5. If the length of a list is odd then the mid-value of the list will be
    the median value.
				
					input_lst = [4, 45, 23, 21, 22, 12, 2]

# sort all list values
sorted_lst = sorted(input_lst)

# get length of sorted list
n = len(sorted_lst)

if n%2 == 0:
    # get first mid value
    mid1 = sorted_lst[n//2]
    # get second mid value
    mid2 = sorted_lst[n//2 -1]
    # average of mid1 and mid2 will be median value
    median = (mid1 + mid2)/2
    print("Median of given list :", median)
else:
    median = sorted_lst[n//2]
    print("Median of given list :",  median)
				
			

Output :

				
					# input_lst = [4, 45, 23, 21, 22, 12, 2]

Median of given list : 21.5
				
			

Related Articles

99. Problem to round every number in a given list of numbers

In this Python list program, we will take a user input as a list and round every number in a given list of numbers, and print the total sum of the list with the help of the below-given steps.

Round every number in list:

Steps to solve the program
  1. Take a list having positive, negative, and float numbers as input and create an empty list.
  2. Use for loop to iterate over each value of the list and round it by using the round() function.
  3. Add the rounded number to the empty list.
  4. Calculate the sum of the new list.
  5. Print the sum.
				
					#Input list
list1 = [22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
list2 = []

for value in list1:
    list2.append(round(value))
    
#Printing output
total = (sum(list2))
print(total)
				
			

Output :

				
					27
				
			

Related Articles

Python Program to get the Median of all the elements from the list.

Python Program to get the Standard deviation of the list element.

Python program to convert all numbers to binary format from a given list.

Python program to convert all the numbers into Roman numbers from the given list.

Python program to calculate the square of each number from the given list.

Python program to combine two lists.

 

 

 

Decode a run-length encoded list

98. Problem to decode a run-length encoded list.

In this Python list program, we will take a run-length encoded list as input and decode it in a list with the help of the below-given steps.

Decode run-length encoded list:

Steps to solve the program
  1. Take a run-length encoded list as input and create an empty list.
  2. Use for loop to iterate over every element from the list.
  3. If an element is encoded list then replace the first element of the list with the second element and add it to the empty list.
  4. Print the list to see the output.
				
					#Input list
list1 = [[2, 1], 2, 3, [2, 4], 5, 1]
list2 = []

for value in list1:
    if isinstance(value,list):
        value[0] = value[1]
        list2.append(value[0])
        list2.append(value[1])
    else:
        list2.append(value)

#Printing output
print(list2)
				
			

Output :

				
					[1, 1, 2, 3, 4, 4, 5, 1]
				
			

Related Articles

Python program to round every number in a given list of numbers and print the total sum of the list.

Python Program to get the Median of all the elements from the list.

Python Program to get the Standard deviation of the list element.

Python program to convert all numbers to binary format from a given list.

Python program to convert all the numbers into Roman numbers from the given list.

Python program to calculate the square of each number from the given list.

 

 

 

Remove duplicate dictionaries from the list

Round every number in a given list of numbers

97. Problem to remove duplicate dictionaries from the list.

In this Python list program, we will take a list containing dictionaries and remove duplicate dictionaries from the list with the help of the below-given steps.

Remove duplicate dictionaries from list:

Steps to solve the program
  1. Take a list containing dictionaries as input and create an empty dictionary.
  2. Use for loop to iterate over each dictionary from the given list.
  3. Add that list to the empty list if it is not in that list.
  4. Print the list to see the output.
				
					#Input list
list1 = [{'Hello': 5},{'student': 7},{'are': 3},{'learning': 8},
         {'Python': 6},{'Its': 3},{'Language': 8},{'are':3}]
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)

#Printing output
print(list2)
				
			

Output :

				
					[{'Hello': 5}, {'student': 7}, {'are': 3}, 
{'learning': 8}, {'Python': 6}, {'Its': 3}, {'Language': 8}]
				
			

Related Articles

Python program to decode a run-length encoded given list.

Python program to round every number in a given list of numbers and print the total sum of the list.

Python Program to get the Median of all the elements from the list.

Python Program to get the Standard deviation of the list element.

Python program to convert all numbers to binary format from a given list.

Python program to convert all the numbers into Roman numbers from the given list.

 

 

 

Get length of each word and add it as dictionary

Decode a run-length encoded list

96. Problem to get the length of each word and add it as dictionary

In this Python list program, we will take a user input as a list and get length of each word and add it as a dictionary with the help of the below-given steps.

Get length of each word in list:

Steps to solve the program
  1. Take a list containing words as input.
  2. Create an empty dictionary and a list.
  3. Use for loop to iterate over each word in the given list and add the word as key in the dictionary and its length as value.
  4. Add that dictionary to the list.
  5. Print the list to see the output.
				
					#Input list
list1 = ["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]
dictionary = dict()
list2 = []

for value in list1:
    dictionary[value]=len(value)
list2.append(dictionary)

#Printing list
print(list2));
				
			

Output :

				
					[{'Hello': 5, 'student': 7, 'are': 3, 'learning': 8, 
'Python': 6, 'Its': 3, 'Language': 8}]
				
			

Related Articles

Python program to remove duplicate dictionaries from the given list.

Python program to decode a run-length encoded given list.

Python program to round every number in a given list of numbers and print the total sum of the list.

Python Program to get the Median of all the elements from the list.

Python Program to get the Standard deviation of the list element.

Python program to convert all numbers to binary format from a given list.

 

 

 

Remove the 2nd character of each word from list

Remove duplicate dictionaries from the list

95. Problem to remove the 2nd character of each word from list.

In this Python list program, we will take a user input as a list and remove the 2nd character of each word from the list with the help of the below-given steps.

Remove the 2nd character from words in list:

Steps to solve the program
  1. Take a list of words as input.
  2. Use a for loop to iterate over words in the list.
  3. Remove the 2nd character of each word from the given list using indexing.
  4. After removing the character add those words to another list.
  5. Print the list to see the output.
				
					#Input list
list1 = ["Hello", "student", "are", "learning", 
         "Python", "Its", "Python", "Language"]
list2 = []

for value in list1:
    list2.append(value[:1]+value[2:])

#printing output
print(list2)
				
			

Output :

				
					['Hllo', 'sudent', 'ae', 'larning', 'Pthon', 
'Is', 'Pthon', 'Lnguage']
				
			

Related Articles

Convert 3rd character of each word to capital

Get length of each word and add it as dictionary

94. Problem to convert the character of each word to capital letter

In this Python list program, we will take a user input as a list and convert 3rd character of each word to the capital with the help of the below-given steps.

The character of each word to capital letter:

Steps to solve the program
  1. Take a list containing words as input.
  2. Use a for loop to iterate over words in the list.
  3. If the length of the word is greater than 3 then convert the 4th character of each word to capital using indexing and the upper() function.
  4. If not then add that word to the empty list.
  5. Add the new words to another list.
  6. Print the list to see the result.
				
					#Input list
list1 = ["Hello", "student", "are", "learning", 
         "Python", "Its", "Python", "Language"]
list2 = []
for value in list1:
    if len(value)>3:
        list2.append(value[:3]+value[3].upper()
                     +value[4:])
    else:
        list2.append(value)
        
#Printing output
print(list2)
				
			

Output :

				
					['HelLo', 'stuDent', 'are', 'leaRning', 'PytHon', 'Its', 'PytHon', 'LanGuage']
				
			

Related Articles

Replace ‘Java’ with ‘Python’ from the given list

Remove the 2nd character of each word from list

93. Problem to replace Java with Python from the given list.

In this Python lost program, we will take a user input as a list and replace Java with Python from the given list with the help of the below-given steps.

Replace Java with Python in a list:

Steps to solve the program
  1. Take a list containing words as input.
  2. Use a for loop with the range function to iterate over words in the list.
  3. If a word is equal to Java replace it with Python.
  4. Print the list after replacing it with the above words to see the output.
				
					#Input list
list1 = ["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]
for i in range(len(list1)):
    if list1[i] == "Python":
        list1[i] = "Java"

#Printing output
print(list1)
				
			

Output :

				
					['Hello', 'student', 'are', 'learning', 'Python',
'Its', 'Python', 'Language']
				
			

Related Articles

Create a dictionary from a sublist in given list

Convert 3rd character of each word to capital

92. Problem to create a dictionary from a sublist in given list

In this Python list program, we will take a list of sublists as input and create a dictionary from a sublist in given list with the help of the below-given steps.

Create dictionary from a sublist:

Steps to solve the program
  1. Take a list of sublists as input.
  2. Convert the given list into a dictionary using dict() and assign it to a variable.
  3. Print the variable to see the result.
				
					#Input list
list1 = [["a", 5], ["b", 8], ["c", 11], ["d", 14], ["e", 23]]

#Converting to dictionary
dictionary = dict(list1)

#Printing output
print(dictionary)
				
			

Output :

				
					{'a': 5, 'b': 8, 'c': 11, 'd': 14, 'e': 23}
				
			

Related Articles

Convert tuples in list into a sublist

Replace ‘Java’ with ‘Python’ from the given list

91. Problem to convert tuples in list into a sublist.

In this Python list program, we will take a list of tuples as input and convert tuples in list into a sublist with the help of the below-given steps.

Convert tuples in list to sublist:

Steps to solve the program
  1. Take a list containing tuples as input.
  2. Using for loop convert tuples in list to sublist.
  3. If the iterated element has type tuple then use nested for lopp to iterate over elements in the tuple.
  4. Add the elements to the empty list using the append() function
  5. And add those lists as sublists to another list.
  6. Print the list to see the result.
				
					#input list
list1 = [(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]
list2 = []

for value in list1:
    if type(value) is tuple:
        list3 = []
        for var in value:
            list3.append(var)
        list2.append(list3)

#Printing output
print(list2)
				
			

Output :

				
					[[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]
				
			

Related Articles

Flatten given nested list structure

Create a dictionary from a sublist in given list