0% found this document useful (0 votes)
100 views

Python List Exercise

The document provides examples of list operations in Python including reversing a list, replacing values, concatenating lists, transforming list items, nested list operations, and filtering lists.

Uploaded by

shubhamcollege11
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)
100 views

Python List Exercise

The document provides examples of list operations in Python including reversing a list, replacing values, concatenating lists, transforming list items, nested list operations, and filtering lists.

Uploaded by

shubhamcollege11
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
You are on page 1/ 3

Exercise 1: Reverse a given list in Python

aList = [100, 200, 300, 400, 500]

Expected output:

aList = [500, 400, 300, 200, 100]

Exercise 2: Given a Python list, find value 20 in the list, and if it is present, replace it with 200. (Only update the
first occurrence of a value.)

Given

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

Expected output:

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

Exercise 3: Concatenate two lists index-wise.

Given:

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

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

Expected output:

list3 = ['My', 'name', 'is', 'Kelly']

Exercise 4: Given a Python list of numbers. Turn every item of a list into its square

Given:

aList = [1, 2, 3, 4, 5, 6, 7]

Expected output:

aList = [1, 4, 9, 16, 25, 36, 49]

Exercise 5: Concatenate two lists in the following order

Given:

list1 = ["Hello ", "take "]

list2 = ["Dear", "Sir"]

Expected output:

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


Exercise 6: Given a two Python list. Iterate both lists simultaneously such that list1 should display item in original
order and list2 in reverse order.

Given

list1 = [10, 20, 30, 40]

list2 = [100, 200, 300, 400]

Expected output:

10 400

20 300

30 200

40 100

Exercise 7: Remove empty strings from the list of strings

Given:

list1 = ["Mike", "", "Emma", "Kelly", "", "Brad"]

Expected output:

list1 = ["Mike", "Emma", "Kelly", "Brad"]

Exercise 8: Add item 7000 after 6000 in the following Python List

Given:

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

Expected output:

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

Exercise 9: Given a nested list extend it by adding the sub list ["h", "i", "j"] in such a way that it will look like the
following list

Given List:

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

Sub List to be added = ["h", "i", "j"]

Expected output:

['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']
Exercise 12: Given a Python list, remove all occurrence of 20 from the list.

Given:

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

Expected output:

[5, 15, 25, 50]

Exercise 13: Write a Python program to count the number of strings where the string length is 3 or more and the first
and last character are same from a given list of strings. Go to the editor

Given:

list1 = ['abc', 'xyz', 'aba', '1221', ‘xyyxz’, ‘AA’]

Expected output: 2

You might also like