Monday, April 29, 2019

Python Important business operation(Reverse the string and Remove the duplicates)

How to remove the duplicates:
>>> mylist=["a","b","a","c","c"]
>>> mylist=list(dict.fromkeys(mylist))
>>> print(mylist)
['a', 'b', 'c']

How to remove duplicates through Function
>>> def my_function(x):
...     return list(dict.fromkeys(x))
...
>>> mylist=my_function(["a","b","a","c","c"])
>>> print(mylist)
['a', 'b', 'c']

Reverse the String:
>>> txt="Hello World"[::-1]
>>> print(txt)
dlroW olleH

Reverse a String through Function:
>>> def my_function(x):
...     return x[::-1]
...
>>> mytxt=my_function("I wonder how this text looks like backwards")
>>> print(mytxt)
sdrawkcab ekil skool txet siht woh rednow I

No comments:

Post a Comment

Python Challenges Program

Challenges program: program 1: #Input :ABAABBCA #Output: A4B3C1 str1="ABAABBCA" str2="" d={} for x in str1: d[x]=d...