40. Problem to remove all odd index elements.

In this program, we will take a user input as a list and remove all odd index elements from that list with the help of the below-given steps.

Remove odd index elements from a list:

Steps to solve the program
  1. Take a list as input.
  2. Create an empty list.
  3. Use for loop with range function to remove all odd index elements.
  4. Add the remaining elements to the empty list.
  5. Print the output to see the result.
				
					Input list
list1 = [12, 32, 33, 5, 4, 7, 33]

#Creating an empty list
list2 = []

for i in range(len(a)):
    if i%2 == 0:
        list2.append(list1[i])

#printing output
print(list2)
				
			

Output :

				
					[12, 33, 4, 33]
				
			

Related Articles

Check the given element exists in the list

Return true if one common member in lists

39. Problem to check the given element exists in the list.

In this program, we will take a user input as a list and that if the given element exists in the list or not with the help of the below-given steps.

Element exists in the list:

Steps to solve the program
  1. Take a list as input.
  2. Use the membership operator i.e. in or not in to check whether an element exists in the list or not.
  3. Print the output to see the result.
				
					#Input list
list1 = [22,45,67,11,90,67]

#Printing output
print("Is 67 exsist in list1? ", 67 in list1)
print("Is 15 exsist in list1? ", 15 in list1)
				
			

Output :

				
					Is 67 exsist in list1?  True
Is 15 exsist in list1?  False
				
			

Related Articles

Replace the last and the first number of the list

Remove all odd index elements

38. Problem to replace the last and the first number of the list

In this program, we will take a user input as a list and replace the last and the first number of the list with the word with the help of the below-given steps.

Replace the last and the first number in a list:

Steps to solve the program
  1. Take a list as input.
  2. Replace the 1st element i.e. element with index value 0 with “SQA”.
  3. Replace the last element i.e. element with index value -1 in the reverse order with “TOOLS”.
  4. Print the list to see the output i.e. replace the last and the first number with a word.
				
					#Input list
list1 = [12, 32, 33, 5, 4, 7]

#Replacing first and last element
list1[0] = "SQA"
list1[-1] = "TOOLS"

#Printing output
print(list1)
				
			

Output :

				
					['SQA', 32, 33, 5, 4, 'TOOLS']
				
			

Related Articles

Get all the unique numbers in the list

Check the given element exists in the list

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