90. Problem to flatten given nested list structure.

In this Python list program, we will take a user input as a nested list and flatten given nested list structure with the help of the below-given steps.

Flatten given nested list:

Steps to solve the program
  1. Take a nested list as input.
  2. Flatten given nested into a single list using for loop.
  3. If the iterated element is a list then add the elements in that list to the empty list.
  4. Use the type() function to check for list type.
  5. If not then also add the element to the empty list.
  6. Print the list to see the result.
#Input lists
list1 = [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
list2 = []

for value in list:
    if type(value) is list:
        for var in value:
            list2.append(var)
    else:
        list2.append(value)

#Printing output
print(list2)

Output :

[0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]

Related Articles

Get the list of palindrome strings

Convert tuples in list into a sublist

89. Problem to get the list of palindrome strings.

In this Python list program, we will take a user input as a list and get the list of palindrome strings from the given list with the hep of the below-given steps.

List of palindrome strings:

Steps to solve the program
  1. Take a list of strings as input and create an empty string to create a list of palindrome strings.
  2. Use for loop to iterate over each string from the list
  3. During iteration check, if the string is equal to its reverse order.
  4. If yes then add that string to the empty list.
  5. Print the list to see the result.
#Input lists
list1 = ["data", "python", "oko", "test", "ete"]
list2 = []

for value in list1:
    new = value[::-1]
    if new == value:
        list2.append(value)

#Printing output
print(list2)

Output :

['oko', 'ete']

Related Articles

Calculate marks percentage from the given list

Flatten given nested list structure

88. Problem to calculate marks percentage from the given list

In this Python list program, we will take a user input as a list and calculate marks percentage from a given mark list, the max mark for each item is 100 with the help of the below-given steps.

Calculate marks percentage:

Steps to solve the program
  1. Take a list containing marks as input.
  2. Calculate the percentage based on the marks in the list using general formula.
  3. Print the output. 
#Input list
list1 = [80, 50, 70, 90, 95]

#Printing output
print("Percentage: ", (sum(list1)/500)*100)

Output :

Percentage:  77.0

Related Articles

Calculate bill per fruit purchased from a list

Get the list of palindrome strings

87. Problem to calculate bill per fruit purchased from a list.

In this Python list program, we will take a user input as a list and calculate the bill per fruit purchased from a given fruit list with the help of the below-given steps.

Bill per fruit purchased:

Steps to solve the program
  1. Take a list of fruits and their prices as input.
  2. Calculate the bill per fruit purchased using for loop.
  3. Print the output.
#Input lists
list1 = [["apple", 30], ["mango", 50], ["banana", 20], ["lichi", 50]]
list2 = [["apple", 2],["mango",10]]

for value in list1:
    for var in list2:
        if value[0] == var[0]:
        #Printing output
            print("Fruit: ", value[0])
            print("Bill: ", value[1]*var[1])

Output :

Fruit:  apple
Bill:  60
Fruit:  mango
Bill:  500

Related Articles

Insert sublist into the list at specific index

Calculate marks percentage from the given list

86. Problem to insert sublist into the list at specific index.

In this Python list program, we will take a user input as a list and insert sublist into the list at specific index with the help of the below-given steps.

Insert sublist in a list:

Steps to solve the program
  1. Take a list as input.
  2. Insert a sublist into the given list at a specific index using the insert() function.
  3. Print the list to see the output.
#Input list
list1 = [4, 6, 8, 2, 3, 5]

#Inserting list
list1.insert(3,[5, 2, 6])

#Printing output
print(list1)

Output :

[4, 6, 8, [5, 2, 6], 2, 3, 5]

Related Articles

Remove duplicate items from the list using set

Calculate bill per fruit purchased from a list

85. Problem to remove duplicate items from the list using set.

In this Python list program, we will take a user input as a list and remove duplicate items from the list using set with the help of the below-given steps.

Remove duplicate items from list:

Steps to solve the program
  1. Take a list as input.
  2. Add elements from the given list into the new list after removing duplicate items using the set() function.
  3. Print the new list to see the output.
#Input list
list1 = [2, 5, 7, 8, 2, 3, 4, 12, 5, 6]

#Set method
list2 = list(set(list2))

#printing output
print(list2)

Output :

[2, 3, 4, 5, 6, 7, 8, 12]

Related Articles

Create a dictionary with lists

Insert sublist into the list at specific index

84. Problem to create a dictionary with lists.

In this Python list program, we will take two user inputs as a list and create a dictionary with lists with the help of the below-given steps.

Create a dictionary with lists:

Steps to solve the program
  1. Take two lists as inputs.
  2. Combine those two lists using the zip function and converts the combined list into the dictionary.
  3. Print the dictionary to see the result.
#Input lists
list1 = ["a", "b", "c", "d", "e"]
list2 = [234, 123, 456, 343, 223]

#Combining lists and
#Converting to dictionary
dictionary = dict(zip(list1,list2))

#Printing output
print(dictionary)

Output :

{'a': 234, 'b': 123, 'c': 456, 'd': 343, 'e': 223}

Related Articles

List after removing n elements from both sides

Remove duplicate items from the list using set

83. Problem to get a list after removing n elements from both sides.

In this Python list program, we will take a user input as a list and get a list after removing n elements from both sides with the help of the below-given steps.

Removing n elements from a list:

STeps to solve the program
  1. Take a list as input.
  2. Print the list after removing n elements from both sides using indexing.
  3. Print the result to see the output.
#Input list
list1 = [2, 5, 7, 9, 3, 4]

#Printing output
print("Remove 1 element from left", list1[1:])
print("Remove 1 element from the right", list1[:-1])
print("Remove 2 elements from left" , list1[2:])
print("Remove 2 elements from right", list1[:-2])

Output :

Remove 1 element from left [5, 7, 9, 3, 4]
Remove 1 element from the right [2, 5, 7, 9, 3]
Remove 2 elements from left [7, 9, 3, 4]
Remove 2 elements from right [2, 5, 7, 9]

Related Articles

Get the list of prime numbers in a given list

Create a dictionary with lists

82. Problem to get the list of prime numbers from a list.

In this Python list program, we will take a user input as a list and get the list of prime numbers in a given list with the help of the below-given steps.

List of prime numbers:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Check whether the element from the given list is a prime number or not using for loop.
  3. If it is a prime number then add that element to the empty list.
  4. Print the list to see the output.
#Input lists
list1 = [11, 8, 7, 19, 6, 29]
list2 = []

for value in list1:
    c=0
    for j in range(1,value):
        if value%j == 0:
            c += 1
    if c == 1:
        list2.append(value)

#Printing output
print(list2)

Output :

[11, 7, 19, 29]

Related Articles

Count the total number of vowels in a list

List after removing n elements from both sides

81. Problem to count the total number of vowels in a list.

In this Python list program, we will take a user input as a list and count the total number of vowels in a list with the help of the below-given steps.

Total number of vowels in a list:

  1. Take a list of words as input.
  2. Create a variable and assign its value equal to ‘aeiouAEIOU’
  3. Join the words in the string using join() and assign them to a variable.
  4. Using for loop and an if statement check whether vowels exist in the joined string.
  5. Count the total number of vowels.
  6. Print the output to see the result.
#Input list
list1 = ["Learning", "Python", "From", "SqaTool"]
vowels = 'aeiouAEIOU'
count = 0

for word in list1:
    for char in word:
        if char in vowels:
            count += 1

#Printing output
print(count)

Output :

8

Related Articles

Print palindrome numbers from a given list

Get the list of prime numbers in a given list