Discussion Forum | Board Infinity

Explain the basic programs of checking unique values, larger numbers in a list, adding all elements of a list, sort the string and reverse the string

Checking unique values
Larger numbers in a list
Adding all elements of a list
Sort the string
Reverse the string

  • Unique Value Program

def unique(list1):

intilize a null list

unique_list = []

traverse for all elements

for x in list1:

check if exists in unique_list or not

if x not in unique_list:
unique_list.append(x)

print list

for x in unique_list:
print x,

list1 = [10, 20, 10, 30, 40, 40]

print(“the unique values from 1st list is”)

unique(list1)

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

print("\nthe unique values from 2nd list is")

unique(list2)

Output:

the unique values from 1st list is

10 20 30 40

the unique values from 2nd list is

1 2 3 4 5

  • Larger Number in list

lst = []

num = int(input('How many numbers: '))

for n in range(num):

numbers = int(input('Enter number '))

lst.append(numbers)

print(“Maximum element in the list is :”, max(lst))

adding all elements of a list

lst = []

num = int(input('How many numbers: '))

for n in range(num):

numbers = int(input('Enter number '))

lst.append(numbers)
print(“Sum of elements in given list is :”, sum(lst))

  • sort the string

a = (“b”, “g”, “a”, “d”, “f”, “c”, “h”, “e”)

x = sorted(a)
print(x)

  • reverse the string

txt = “Hello World”[ : :-1]

print(txt)