[python] – List

by | Feb 4, 2024 | Ngôn ngữ lập trình, Python | 0 comments

Python list is the most widely used data structure, and a good understanding of it is necessary. This Python list exercise aims to help developers learn and practice list operations. All questions are tested on Python 3.

Questions cover the following list topics:

  • list operations and manipulations
  • list functions
  • list slicing
  • list comprehension

Exercise 1: Reverse a list

Example

Input

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

Output

[500, 400, 300, 200, 100]

Hint:

  • Use the list function reverse()

Solution:

Solution 1: list function reverse()

list1 = [100, 200, 300, 400, 500]
list1.reverse()
print(list1)

Solution 2: Using negative slicing

-1 indicates to start from the last item.

list1 = [100, 200, 300, 400, 500]
list1 = list1[::-1]
print(list1)

Exercise 2: Concatenate two lists index-wise

Write a program to add two lists index-wise. Create a new list that contains the 0th index item from both the list, then the 1st index item, and so on till the last element. any leftover items will get added at the end of the new list.

Example

Input

list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]

Output

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

Hint:

  • Use list comprehension with the zip() function

Solution:

Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.

list1 = ["M", "na", "i", "Ke"] 
list2 = ["y", "me", "s", "lly"]
list3 = [i + j for i, j in zip(list1, list2)]
print(list3)

Exercise 3: Turn every item of a list into its square

Given a list of numbers. write a program to turn every item of a list into its square.

Example

Input

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

Output

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

Hint:

  • Iterate numbers from a list one by one using a for loop and calculate the square of the current number

Solution:

Solution 1: Using loop and list method

  • Create an empty result list
  • Iterate a numbers list using a loop
  • In each iteration, calculate the square of a current number and add it to the result list using the append() method.
numbers = [1, 2, 3, 4, 5, 6, 7]
# result list
res = []
for i in numbers:
    # calculate square and add to the result list
    res.append(i * i)
print(res)

Solution 2: Use list comprehension

numbers = [1, 2, 3, 4, 5, 6, 7]
res = [x * x for x in numbers]
print(res)

Exercise 4: Concatenate two lists in the following order

Example

Input

list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]

Output

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

Hint:

  • Use a list comprehension to iterate two lists using a for loop and concatenate the current item of each list.

Solution:

list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]

res = [x + y for x in list1 for y in list2]
print(res)

Exercise 5: Iterate both lists simultaneously

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.

Example

Input

list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]

Output

10 400
20 300
30 200
40 100

Hint:

  • Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.

Solution:

  • The zip() function can take two or more lists, aggregate them in a tuple, and returns it.
  • Pass the first argument as a list1 and seconds argument as a list2[::-1] (reverse list using list slicing)
  • Iterate the result using a for loop
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]

for x, y in zip(list1, list2[::-1]):
    print(x, y)

Exercise 6: Remove empty strings from the list of strings

Example

Input

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

Output

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

Hint:

  • Use a filter() function to remove the None / empty type from the list

Solution:

Use a filter() function to remove None type from the list

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

# remove None from list1 and convert result into list
res = list(filter(None, list1))
print(res)

Exercise 7: Add new item to list after a specified item

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

Example

Input

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

Output

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

Hint:

  • The given list is a nested list. Use indexing to locate the specified item, then use the append() method to add a new item after it.

Solution:

Use the append() method

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

# understand indexing
# list1[0] = 10
# list1[1] = 20
# list1[2] = [300, 400, [5000, 6000], 500]
# list1[2][2] = [5000, 6000]
# list1[2][2][1] = 6000

# solution
list1[2][2].append(7000)
print(list1)

Exercise 8: Extend nested list by adding the sublist

You have given a nested list. Write a program to extend it by adding the sublist ["h", "i", "j"] in such a way that it will look like the following list.

Example

Input

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

# sub list to add
sub_list = ["h", "i", "j"]

Output

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

Hint:

  • The given list is a nested list. Use indexing to locate the specified sublist item, then use the extend() method to add new items after it.

Solution:

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

# understand indexing
# list1[2] = ['c', ['d', 'e', ['f', 'g'], 'k'], 'l']
# list1[2][1] = ['d', 'e', ['f', 'g'], 'k']
# list1[2][1][2] = ['f', 'g']

# solution
list1[2][1][2].extend(sub_list)
print(list1)

Exercise 9: Replace list’s item with 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 an item.

Example

Input

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

Output

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

Hint:

  • Use list method index(20) to get the index number of a 20
  • Next, update the item present at the location using the index number

Solution:

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

# get the first occurrence index
index = list1.index(20)

# update item present at location
list1[index] = 200
print(list1)

Exercise 10: Remove all occurrences of a specific item from a list.

Given a Python list, write a program to remove all occurrences of item 20.

Example

Input

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

Output

[5, 15, 25, 50]

Hint:

  • Some hint for student

Solution:

Solution 1: Use the list comprehension

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

# list comprehension
# remove specific items and return a new list
def remove_value(sample_list, val):
    return [i for i in sample_list if i != val]

res = remove_value(list1, 20)
print(res)

Solution 2while loop (slow solution)

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

while 20 in list1:
    list1.remove(20)
print(list1)