0% found this document useful (0 votes)
97 views3 pages

Python List Exercises and Solutions

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)
97 views3 pages

Python List Exercises and Solutions

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

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

Common questions

Powered by AI

You can concatenate two lists index-wise using a list comprehension with a 'zip()' function: '[i + j for i, j in zip(list1, list2)]'. For the lists ['M', 'na', 'i', 'Ke'] and ['y', 'me', 's', 'lly'], the output is ['My', 'name', 'is', 'Kelly'].

Use the 'zip()' function with slicing to reverse the second list: 'for x, y in zip(list1, list2[::-1])'. For lists [10, 20, 30, 40] and [100, 200, 300, 400], the paired output is 10 400, 20 300, 30 200, 40 100 .

Remove empty strings using list comprehension with a condition: '[s for s in list1 if s]'. This applied to ['Mike', '', 'Emma', 'Kelly', '', 'Brad'] results in ['Mike', 'Emma', 'Kelly', 'Brad'].

Count such strings using a list comprehension with a condition: 'len([s for s in list1 if len(s) >= 3 and s[0] == s[-1]])'. For ['abc', 'xyz', 'aba', '1221', 'xyyxz', 'AA'], there are 2 strings ('aba', '1221') fulfilling the criteria .

To concatenate two lists in all possible orders, use a nested loop within a list comprehension: '[x + y for x in list1 for y in list2]'. For ['Hello ', 'take '] and ['Dear', 'Sir'], the output would be ['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir'].

Use a list comprehension to iterate over each element and apply the square operation: '[x ** 2 for x in aList]'. Applying this to [1, 2, 3, 4, 5, 6, 7] results in [1, 4, 9, 16, 25, 36, 49].

Access the desired position by indexing into the nested list, then use 'insert()': 'list1[2][2].insert(2, 7000)'. For the list [10, 20, [300, 400, [5000, 6000], 500], 30, 40], this process inserts 7000, resulting in [10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40].

To reverse a list in Python, you can use the slicing method with a step of -1: 'aList[::-1]', where 'aList' is the list you want to reverse. Applying this to [100, 200, 300, 400, 500] results in [500, 400, 300, 200, 100].

To replace the first occurrence of a specific value in a list, you can use the 'index()' method to find the position of the value and then update it. For the list [5, 10, 15, 20, 25, 50, 20], use 'list1[list1.index(20)] = 200'. This gives [5, 10, 15, 200, 25, 50, 20].

Navigate to the target sublist with appropriate indexing and use 'extend()': 'list1[2][1][2].extend(sub_list)'. Applying this to [['a', 'b', ['c', ['d', 'e', ['f', 'g'], 'k'], 'l'], 'm', 'n']] with ['h', 'i', 'j'], the output is ['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n'].

You might also like