From list of strings, length of longest string

In this program, we will take a list of strings as input and returns the length of the longest string.

Steps to solve the program
  1. Take a list of strings as input.
  2. Create a variable named temp and assign its value equal to zero.
  3. Use for loop to iterate over each string of the list.
  4. Check if the length of the string is greater than the temp value, if yes assign the length of that string to the temp variable.
  5. Print the length of the longest string.
				
					#input string
string = ["i", "am", "learning", "python"]
temp = 0

for word in string:
    a = len(word)
    if a > temp:
        temp = a
        
#Printing output
print(temp)
				
			

Output :

				
					8
				
			

string made of the first and the last 2 characters

string made of 4 copies of the last two characters

Get a string made of first and the last 2 chars

In this program, we will take a string as input and get a string made of the first and the last 2 chars from a given string

Steps to solve the program.
  1. Take a string as input.
  2. If the string consists of only 2 characters print True if not get a string made of the first and last 2 characters using indexing.
  3. Print the output.
				
					#Input string
string = "sqatools"

#Printing output
if len(string) < 2:
    print(True)
else:
    print(string[:2]+string[-2:])
				
			

Output :

				
					sqls
				
			

list of strings and returns the length of the longest string.

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