37. Problem to convert a string into a list.

In this program, we will take a user input as a string and convert that string into a list with the help of the below-given steps.

Convert a string into a list:

Steps to solve the program
  1. Take a string as input.
  2. Split the given string using the split function and store it in a variable.
  3. Print the variable to see the output.
				
					#Input string
string = "i am learning python"

#Splitting the string
list1 = string.split(" ")

#Printing output
print(list1)
				
			

Output :

				
					['i', 'am', 'learning', 'python']
				
			

Related Articles

Get all the unique numbers in the list

Replace the last and the first number of the list

36. Problem to get all the unique numbers in the list.

In this program, we will take a user input as a list and find all the unique numbers in the list with the help of the below-given steps. There are two methods to solve this problem.

Table of Contents

Unique numbers in a list:

Method 1 :  Using in-built set function

Steps to solve the program
  1. Create a list.
  2. Use the set() function to find the all unique numbers in the list.
  3. Convert the set back into a list using the list() function.
  4. Print the list to see the output.
				
					#Input list
list1 = [12,34,78,12,45,34]

#Printing output
print(list(set(list1)))
				
			

Output :

				
					[34, 12, 45, 78]
				
			

Method 2 : Using for loop

Steps to solve the program
  1. Create a list and an empty list.
  2. Use for loop to check whether an element from list1 exists in list2.
  3. Use the if statement for this purpose. 
  4. If not add that element to the list2.
  5. Print list2.
				
					#Input list
list1 = [12,34,78,12,45,34]

#Creating empty list
list2 = []

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

#Printing output
print(list2)
				
			

Output :

				
					[12, 34, 78, 45]

				
			

Related Articles

Get keys and values from the list of dictionaries

Convert a string into a list

35. Problem to get keys and values from the list of dictionaries.

In this program, we will take input as a list of dictionaries. Get the keys and values from the list of dictionaries using for loop with the help of the below-given steps.

Get keys and values from the list of dictionaries:

  1. Take a list of dictionaries as input.
  2. Create two empty lists named key and value to store keys and values.
  3. Use for loop iterate over the list of a dictionary.
  4. Use another for loop inside the above loop to add the key and value of the dictionary element into the empty list.
  5. Print both lists to see the output.
				
					#Input list
list1 = [{"a":12}, {"b": 34}, {"c": 23}, {"d": 11}, {"e": 15}]

#Creating empty list
key = []
value = []
for element in list1:
    for val in element:
        key.append(val)
        value.append(element[val])

#Print output
print(key)
print(value)
				
			

Output :

				
					['a', 'b', 'c', 'd', 'e']

[12, 34, 23, 11, 15]
				
			

Related Articles

Combine two list elements as a sublist

Get all the unique numbers in the list

34. Problem to combine two list elements as a sublist.

We will take two lists as input & iterate all the elements one by one with the Python loop. Combine two list elements from both lists as a sublist with the help of the below-given steps.

Combine two list elements:

  1. Take two lists as input.
  2. combine both lists using zip().
  3. Add the combined elements to an empty list.
  4. Use for loop for this purpose.
  5. Print the list to see the output.
				
					#Input lists
list1 = [3, 5, 7, 8, 9]
list2 = [1, 4, 3, 6, 2]

#Creating empty list
final = []

for (a,b) in zip(list1,list2):
    final.append(list((a,b)))

#Printing output
print(final)
				
			

Output :

				
					[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
				
			

Related Articles

Reverse each element of the list

Get keys and values from the list of dictionaries

33. Problem to reverse each element of the list.

In this program, we will take a user input as a list and reverse each element of the list with the help of the below-given steps.

Reverse each element in a list:

Steps to solve the program
  1. Take a list of words as input and create an empty list.
  2. Use for loop to iterate over each element of the list.
  3. Reverse each element using Indexing.
  4. After reversing the word add it to the empty list.
  5. Print the list to see the output.
				
					#Input list
list1 = ["Sqa","Tools","Online","Learning","Platform’"]

#Creating an empty list
list2 = []

for word in list1:
    list2.append(word[::-1])

#Printing output
print(list2)
				
			

Output :

				
					['aqS', 'slooT', 'enilnO', 'gninraeL', '’mroftalP']
				
			

Related Articles

difference between two lists

Combine two list elements as a sublist

32. Problem to get the difference between two lists.

In this program, we will take two lists as input. Find the difference between two lists with the help of the below-given steps.

Difference between two lists:

Steps to solve the program
  1. Take two lists as input.
  2. Print the elements from the list1 who are not in list2.
  3. Use for loop and an if statement for this purpose.
				
					#Input list
list1 = [34,55,12,89,44,67]
list2 = [55,89,34]

for value in list1:
    if value not in list2:
    #Printing output
        print(value, end=" ")
				
			

Output :

				
					12 44 67
				
			

Related Articles

Merge all elements using special character

Reverse each element of the list

31. Problem to merge all elements using a special character

In this program, we will take a user input as a list and merge all elements of the list in a single entity using a special character with the help of the below-given steps.

Merge all elements in a list:

Steps to solve the program
  1. Take a list of characters as input.
  2. Join the characters from the list using join() to merge all elements.
  3. Use “$” to join the character.
  4. Print the output.
				
					#Input list
list1 = ["a","b","c","d"]

#Joining and printing output
print("$".join(list1))
				
			

Output :

				
					a$b$c$d
				
			

Related Articles

Find the second smallest number from the list

Get the difference between two lists

30. Problem to find the second smallest number from the list.

In this program, we will take a user input as a list. Find the second smallest number from the list with the help of the below-given steps.

Second smallest number in a list:

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using sort().
  3. Print the second smallest number from the list using indexing.
				
					#Input list
list1 = [22,11,78,45,90,44]

#Sorting the list
list1.sort()

#Printing output
print("2nd smallest number: ", list1[1])
				
			

Output :

				
					2nd smallest number:  22
				
			

Related Articles

Find the second largest number from the list

Merge all elements using special character

29. Problem to find the second largest number in the list.  

In this program, we will take a user input as a list. Find the second largest number from the list with the help of the below-given steps.

Second largest number in a list:

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using sort().
  3. Print the second largest number from the list using indexing.
				
					#Input list
list1 = [22,11,78,45,90,44]

#sorting the list
list1.sort()

#printing the output
print("2nd largest number: ", list1[-2])
				
			

Output :

				
					2nd largest number:  78
				
			

Related Articles

Generate all sublists with 5 or more elements

Find the second smallest number from the list

28. Problem to generate all sublists with 5 or more elements.

In this program, we will take a user input as a list. Generate all sublists having 5 or more elements in it from the given list with the help of the below-given steps.

Generate all sublists of a list:

Steps to solve the program
  1. Take a list as input and create an empty list to store and generate all sublists.
  2. From itertools import combinations for generating sublists with 5 or more elements.
  3. Use for loop and range function for this purpose.
  4. If the generated list contains 5 or more elements add that list as a sublist to the empty list.
  5. Print the list to see the output.
				
					#Importing combinations
from itertools import combinations

#Input list
list1 = [1,3,5,8,0,22,45]

#Creating an empty list
sub = []

for i in range(0,len(list1)+1):
#Checking for combinations
    for sublist in combinations(list1,i):
        temp = list(sublist)
        if len(temp) > 4:
            sub.append(temp)

#Printing output
print(sub)
				
			

Output :

				
					[[1, 3, 5, 8, 0],
 [1, 3, 5, 8, 22],
 [1, 3, 5, 8, 45],
 [1, 3, 5, 0, 22],
 [1, 3, 5, 0, 45],
 [1, 3, 5, 22, 45],
 [1, 3, 8, 0, 22],
 [1, 3, 8, 0, 45],
 [1, 3, 8, 22, 45],
 [1, 3, 0, 22, 45],
 [1, 5, 8, 0, 22],
 [1, 5, 8, 0, 45],
 [1, 5, 8, 22, 45],
 [1, 5, 0, 22, 45],
 [1, 8, 0, 22, 45],
 [3, 5, 8, 0, 22],
 [3, 5, 8, 0, 45],
 [3, 5, 8, 22, 45],
 [3, 5, 0, 22, 45],
 [3, 8, 0, 22, 45],
 [5, 8, 0, 22, 45],
 [1, 3, 5, 8, 0, 22],
 [1, 3, 5, 8, 0, 45],
 [1, 3, 5, 8, 22, 45],
 [1, 3, 5, 0, 22, 45],
 [1, 3, 8, 0, 22, 45],
 [1, 5, 8, 0, 22, 45],
 [3, 5, 8, 0, 22, 45],
 [1, 3, 5, 8, 0, 22, 45]]
				
			

Related Articles

Check whether a list contains a sublist

Find the second largest number from the list