Learn how to sort() a list in python,how to convert string to list in python programs and how to append to a list in python.

These are the simple python programs for sorting () list in python.We have listed some comlex python programs and their solutions in which you will learn how to sort() a list in python and how to convert string to list in python and also how to append to a list in python.


Sort() list in python:

These are some practical examples of how to sort () lists in python.

Python Programs to sort() lists and answers:


Q1: Given a two Python list. Write a program to iterate both lists simultaneously and display items from list1 in original order and items from list2 in reverse order.


Solution:

list1 = [1,2,3,4,5,6,7,8]

list2 = ["a","b","c","d","e","f","g","h"]


while len(list1) < len(list2): 

    list1.append(" ")

while len(list1) > len(list2): 

    list1.append(" ")

list2.reverse()


c=0

d=0

for (i,j) in zip(list1,list2):

    print(i,j)

How to sort( ) a list in python?



Q2: Write a program to add item 7000 after 6000 in the following Python List

Given: list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]

Solution:

list1 = [10 , 20 , [300 , 400 , [5000 , 6000 , 500] , 30 , 40]

list1= [2][2].append(7000)

print(list1)

How to sort( ) a list in python?


Q3: Remove all occurrences of a specific item from a list.

write a program that Generate a Python list, and add 5 user input, then removes all occurrences of item user-defined item if exist.

Solution:

list1 = ['hockey','football','tennis','badminton','volleyball','cricket','squash','kabaddi','tennis']

a=1

for x in list1:

    print(x)


while True:

    b=input("do want to remove (y/n): ")

    if b=="y":

        a = input('enter a sport you want to remove: ')

    else:

        break

        while a not in list1: 

        a = input('The sport is not in the list kindly enter sport from the list: ')


    while a in list1:

        l1.remove(a)


print(list1)

How to sort( ) a list in python?


Q4: Write a program that has two lists of numbers, now swap the list item as from list1 every even number item move to the 2nd list at odd number item index, and from 2nd list every odd number item move to the 1st list at even number item's index.

Solution:

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

list2 = [6,7,8,9,1]

tmp=[x for x in list1 if x%2==0]

tmp2=[x for x in list2 if x%2!=0]

print(list1)

print(list2)


count=0

for x in range(len(list2)):

    if x%2!=0:

        l2[x]=tmp[count]

        count+=1

count=0

for x in range(len(list1)):

    if x%2==0:

        l1[x]=tmp2[count]

        count+=1


print(tmp)

print(tmp2)

print("List 1",list1)

print("List 2",list2)

How to sort( ) a list in python?


Online python compilers:

https://replit.com/languages/online-python-compiler

https://www.programiz.com/python-programming/online-compiler/