0% found this document useful (0 votes)
75 views4 pages

2.1 Python LIST Exercise

This document provides 10 exercises to practice working with Python lists. The exercises include: 1) reversing a list, 2) concatenating two lists index-wise, 3) returning the square of each item in a list, 4) concatenating two lists in a specific order, 5) iterating over two lists simultaneously, 6) removing empty strings from a list, 7) adding an item to a nested list at a specified position, 8) extending a nested list by adding a sublist, 9) replacing the first occurrence of an item with a new value, and 10) removing all occurrences of a specific item from a list. The document provides sample input and expected output for each exercise.

Uploaded by

paridhinimje0212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views4 pages

2.1 Python LIST Exercise

This document provides 10 exercises to practice working with Python lists. The exercises include: 1) reversing a list, 2) concatenating two lists index-wise, 3) returning the square of each item in a list, 4) concatenating two lists in a specific order, 5) iterating over two lists simultaneously, 6) removing empty strings from a list, 7) adding an item to a nested list at a specified position, 8) extending a nested list by adding a sublist, 9) replacing the first occurrence of an item with a new value, and 10) removing all occurrences of a specific item from a list. The document provides sample input and expected output for each exercise.

Uploaded by

paridhinimje0212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2.

1 Python list Exercise


1) Write a program to reverse a list in python.

Input:

list1 = [100, 200, 300, 400, 500, 'IOTA']

Expected output:

['IOTA', 500, 400, 300, 200, 100]

2) Write a program to concatenate two lists index-wise.

Input:

list1 = ["M", "na", "i", "IO"]


list2 = ["y", "me", "s", "TA"]

Expected output:

['My', 'name', 'is', 'IOTA']

3) Write a program to return square of every item of a list.

Input:

numbers = [1, 2, 3, 4, 5, 6, 7,10]

Expected output:

[1, 4, 9, 16, 25, 36, 49,100]

This is property of IOTA Academy and can’t be shared with outside people. Any breach to TnC of IOTA will lead to legal proceedings
1
4) Write a program to concatenate two lists in the following
order.

Input:

list1 = ["Hello ", "take "]


list2 = ["Dear", "Sir"]

Expected output:

['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']

5) Write a program to iterate both lists simultaneously.

Input:

list1 = [10, 20, 30, 40]


list2 = [100, 200, 300, 400]

Expected output:

10 400
20 300
30 200
40 100

6) Write a program to remove empty strings from the list of


strings.

Input:

list1 = ["Dhawan", "", "Jadeja", "Dhoni", "", "Virat"]

Expected output:

['Dhawan', 'Jadeja', 'Dhoni', 'Virat']

This is property of IOTA Academy and can’t be shared with outside people. Any breach to TnC of IOTA will lead to legal proceedings

2
7) Write a program to add a new item to the list at a specified
position.

- Add item 7000 after 6000 in the following Python List

Input:

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

Expected output:

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

8) Write a program to extend the nested list by adding a sublist


to it.

Input:

list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "l"], "m", "n"]
sub_list = ["h", "i", "j"]

Expected Output:

['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']

9) Write a program to replace the list’s item with a new value if


found.
- You have given a Python list. Write a program to find value 20 in the list, and if it is
present, replace it with 200. Only update the first occurrence of the item.

Input:

list1 = [5, 10, 15, 20, 25, 50, 20]

Expected output:

[5, 10, 15, 200, 25, 50, 20]

This is property of IOTA Academy and can’t be shared with outside people. Any breach to TnC of IOTA will lead to legal proceedings

3
10) Write a program to remove all occurrences of a specific item
from a list.
- Given a Python list, write a program to remove all occurrences of item 20.

Input:

list1 = [5, 20, 15, 20, 25, 50, 20]

Expected output:

[5, 15, 25, 50]

Good Luck!

This is property of IOTA Academy and can’t be shared with outside people. Any breach to TnC of IOTA will lead to legal proceedings

You might also like