Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Python: List and list slicing

Python lists are mutable type its mean modifiable.

lists store different data type not homogenous

most common and reliable type is the list

ordered and access by index

print("Name : %s, ID: %d, Country: %s"%(emp[0],emp[1],emp[2]))

list[0]||list[0:6]||list[:]||list[2:5]

Iterating a List using for loop
Adding elements to the list using append()
append() method only adds elements to the end of a list
Removing elements from the list using remove()
list.remove(2)
Write the program to remove the duplicate element of the list using for loop and append function.
repetition,iteration,concatenation in list

list

===============================================

Python List Vs Tuple

===============================================

python-tutorials-difference-between-list-array-tuple-set-dict

list

A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created. However, Python consists of six data-types that are capable to store the sequences, but the most common and reliable type is the list.

A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

A list can be define as below

L1 = ["John", 102, "USA"]    
L2 = [1, 2, 3, 4, 5, 6]   
Enter fullscreen mode Exit fullscreen mode

Characteristics of Lists
The list has the following characteristics:

  1. The lists are ordered.
  2. The element of the list can access by index.
  3. The lists are the mutable type.
  4. The lists are mutable types. A list can store the number of various elements. Let's check the first statement that lists are the ordered.
a = [1,2,"Peter",4.50,"Ricky",5,6]  
b = [1,2,5,"Peter",4.50,"Ricky",6]  
a ==b  
Enter fullscreen mode Exit fullscreen mode

Output:

False

Let's have a look at the list example in detail.

emp = ["John", 102, "USA"]     
Dep1 = ["CS",10]  
Dep2 = ["IT",11]    
HOD_CS = [10,"Mr. Holding"]    
HOD_IT = [11, "Mr. Bewon"]    
print("printing employee data...")    
print("Name : %s, ID: %d, Country: %s"%(emp[0],emp[1],emp[2]))    
print("printing departments...")   
print("Department 1:\nName: %s, ID: %d\nDepartment 2:\nName: %s, ID: %s"%(Dep1[0],Dep2[1],Dep2[0],Dep2[1]))    
print("HOD Details ....")    
print("CS HOD Name: %s, Id: %d"%(HOD_CS[1],HOD_CS[0]))    
print("IT HOD Name: %s, Id: %d"%(HOD_IT[1],HOD_IT[0]))    
print(type(emp),type(Dep1),type(Dep2),type(HOD_CS),type(HOD_IT)) 
Enter fullscreen mode Exit fullscreen mode

Output:

printing employee data...
Name : John, ID: 102, Country: USA
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
IT HOD Name: Mr. Bewon, Id: 11
<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>
Enter fullscreen mode Exit fullscreen mode

Consider the following example:

list = [1,2,3,4,5,6,7]  
print(list[0])  
print(list[1])  
print(list[2])  
print(list[3])  
# Slicing the elements  
print(list[0:6])  
# By default the index value is 0 so its starts from the 0th element and go for index -1.  
print(list[:])  
print(list[2:5])  
print(list[1:6:2]) 
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
[2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

Updating List values
Lists are the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator.

Python also provides append() and insert() methods, which can be used to add values to the list.

Consider the following example to update the values inside the list.

list = [1, 2, 3, 4, 5, 6]     
print(list)     
 It will assign value to the value to the second index   
list[2] = 10   
print(list)    
Adding multiple-element   
list[1:3] = [89, 78]     
print(list)   
It will add value at the end of the list  
list[-1] = 25  
print(list) 
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]
Enter fullscreen mode Exit fullscreen mode

Consider the following example to delete the list elements.

list = [1, 2, 3, 4, 5, 6]     
print(list)     
 It will assign value to the value to second index   
list[2] = 10   
print(list)    
 Adding multiple element   
list[1:3] = [89, 78]     
print(list)   
 It will add value at the end of the list  
list[-1] = 25  
print(list)  
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]
Enter fullscreen mode Exit fullscreen mode

Image description

Iterating a List
A list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows.

list = ["John", "David", "James", "Jonathan"]  
Enter fullscreen mode Exit fullscreen mode
for i in list:   
    The i variable will iterate over the elements of the List and contains each element in each iteration.     
    print(i) 
Enter fullscreen mode Exit fullscreen mode

Output:

John
David
James
Jonathan
Enter fullscreen mode Exit fullscreen mode

Adding elements to the list
Python provides append() function which is used to add an element to the list. However, the append() function can only add value to the end of the list.

Consider the following example in which, we are taking the elements of the list from the user and printing the list on the console.

Declaring the empty list

l =[]  
Number of elements will be entered by the user    
n = int(input("Enter the number of elements in the list:"))  
 for loop to take the input  
for i in range(0,n):     
    The input is taken from the user and added to the list as the item  
    l.append(input("Enter the item:"))     
print("printing the list items..")   
 traversal loop to print the list items    
for i in l:   
    print(i, end = "  ")   
Enter fullscreen mode Exit fullscreen mode

Output:

Enter the number of elements in the list:5
Enter the item:25
Enter the item:46
Enter the item:12
Enter the item:75
Enter the item:42
printing the list items
25  46  12  75  42  
Enter fullscreen mode Exit fullscreen mode

Removing elements from the list
Python provides the remove() function which is used to remove the element from the list. Consider the following example to understand this concept.

Example -

list = [0,1,2,3,4]     
print("printing original list: ");    
for i in list:    
    print(i,end=" ")    
list.remove(2)    
print("\nprinting the list after the removal of first element...")    
for i in list:    
    print(i,end=" ")  
Enter fullscreen mode Exit fullscreen mode

Output:

printing original list: 
0 1 2 3 4 
printing the list after the removal of first element...
0 1 3 4 
Enter fullscreen mode Exit fullscreen mode

Image description

Example: 1- Write the program to remove the duplicate element of the list.

list1 = [1,2,2,3,55,98,65,65,13,29]  
 Declare an empty list that will store unique values  
list2 = []  
for i in list1:  
    if i not in list2:  
        list2.append(i)  
print(list2) 
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 55, 98, 65, 13, 29]
Enter fullscreen mode Exit fullscreen mode
Example:2- Write a program to find the sum of the element in the list.

list1 = [3,4,5,9,10,12,24]  
sum = 0  
for i in list1:  
    sum = sum+i      
print("The sum is:",sum) 
Enter fullscreen mode Exit fullscreen mode
Output:

The sum is: 67
Enter fullscreen mode Exit fullscreen mode

Example: 3- Write the program to find the lists consist of at least one common element.

list1 = [1,2,3,4,5,6]  
list2 = [7,8,9,2,10]  
for x in list1:  
    for y in list2:  
        if x == y:  
            print("The common element is:",x)  
Enter fullscreen mode Exit fullscreen mode
Output:

The common element is: 2
Enter fullscreen mode Exit fullscreen mode

MORE EXAMPLES ON IT

Image description

Image description

Image description

Image description

Python List Vs Tuple

Image description

Image description

Image description

Image description

Image description

Image description

list slicing

Basic List Slicing:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[1:4]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Negative Index Slicing:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[-3:-1]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [3, 4]
Enter fullscreen mode Exit fullscreen mode

Slicing with Stride:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[0:5:2]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [1, 3, 5]
Enter fullscreen mode Exit fullscreen mode

Slicing to Copy a List:

original_list = [1, 2, 3, 4, 5]
copied_list = original_list[:]
print(copied_list)
Enter fullscreen mode Exit fullscreen mode
Output: [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Omitting Start and End Indices:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[:]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Slicing with Negative Stride:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[::-1]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

Slicing to Remove Elements:

my_list = [1, 2, 3, 4, 5]
del my_list[1:3]
print(my_list)
Enter fullscreen mode Exit fullscreen mode
Output: [1, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Slicing with Step Greater than List Length:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[::7]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [1]
Enter fullscreen mode Exit fullscreen mode

Slicing to Reverse a List:

my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
print(reversed_list)
Enter fullscreen mode Exit fullscreen mode
Output: [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

Slicing with Overflow Indices:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[1:100]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Slicing with Negative Start Index:

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[-3:]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Slicing a String:

my_string = "Hello, World!"
sliced_string = my_string[7:]
print(sliced_string)
Enter fullscreen mode Exit fullscreen mode
Output: 'World!'
Enter fullscreen mode Exit fullscreen mode

Slicing Nested Lists:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sliced_list = nested_list[1][0:2]
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [4, 5]
Enter fullscreen mode Exit fullscreen mode

Slicing with Conditional Statements:

my_list = [1, 2, 3, 4, 5, 6]
even_numbers = [x for x in my_list if x % 2 == 0]
print(even_numbers)
Enter fullscreen mode Exit fullscreen mode
Output: [2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

Slicing with a Function:

def get_slice(lst, start, end):
    return lst[start:end]

my_list = [1, 2, 3, 4, 5]
sliced_list = get_slice(my_list, 1, 4)
print(sliced_list)
Enter fullscreen mode Exit fullscreen mode
Output: [2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Slicing to Extract Odd Indices:

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_indices = my_list[1::2]
print(odd_indices)
Enter fullscreen mode Exit fullscreen mode

Output: [1, 3, 5, 7, 9]

Slicing to Extract Even Indices:

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
even_indices = my_list[::2]
print(even_indices)
Enter fullscreen mode Exit fullscreen mode
Output: [0, 2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

Slicing to Get First N Elements:

my_list = [1, 2, 3, 4, 5, 6]
n = 3
first_n_elements = my_list[:n]
print(first_n_elements)
Enter fullscreen mode Exit fullscreen mode
Output: [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Slicing to Get Last N Elements:

my_list = [1, 2, 3, 4, 5, 6]
n = 3
last_n_elements = my_list[-n:]
print(last_n_elements)
Enter fullscreen mode Exit fullscreen mode
Output: [4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

=============================================================

List Question

  1. Create an empty list and add integers 1 to 5 to it.
  2. Append a string to an existing list of names.
  3. Given a list of numbers, calculate the sum of all the elements.
  4. Create a list of fruits and check if a specific fruit is present.
  5. Iterate through a list of words and print each word in uppercase.
  6. Write a function to reverse a list without using the reverse method.
  7. Given a list of numbers, find the maximum and minimum values.
  8. Remove the first occurrence of a specific element from a list.
  9. Create a list of names and sort them in alphabetical order.
  10. Count the occurrences of a specific element in a list.
  11. Merge two lists into a new list without duplicates.
  12. Write a function to check if a list is a palindrome.
  13. Given a list of strings, concatenate them into a single string.
  14. Find the index of the first occurrence of a specific element in a list.
  15. Create a list of numbers and filter out the even numbers.
  16. Write a function to remove all occurrences of a specific element from a list.
  17. Calculate the average of elements in a list of numbers.
  18. Create a list of strings and sort them by length.
  19. Write a function to flatten a nested list into a single list.
  20. Given a list of words, create a new list with the lengths of the words.
  21. Find the second largest element in a list of numbers.
  22. Create a list of integers and check if all elements are greater than a specified value.
  23. Write a function to rotate a list by a specified number of positions.
  24. Remove duplicate elements from a list while preserving the order.
  25. Given two lists of numbers, find the common elements between them.
  26. Write a function to split a list into sublists of a specified size.
  27. Shuffle the elements of a list randomly.
  28. Create a list of strings and capitalize the first letter of each string.
  29. Write a function to find the intersection of multiple lists.
  30. Given a list of numbers, calculate the product of all the elements
# Task 1: Create an empty list and add integers 1 to 5 to it.
empty_list = []
for i in range(1, 6):
    empty_list.append(i)
print("Task 1 - List with Integers 1 to 5:", empty_list)

# Task 2: Append a string to an existing list of names.
names_list = ["Alice", "Bob", "Charlie"]
names_list.append("David")
print("Task 2 - Updated Names List:", names_list)

# Task 3: Given a list of numbers, calculate the sum of all the elements.
numbers_list = [10, 20, 30, 40, 50]
sum_of_elements = sum(numbers_list)
print("Task 3 - Sum of Elements in Numbers List:", sum_of_elements)

# Task 4: Create a list of fruits and check if a specific fruit is present.
fruits_list = ["apple", "banana", "cherry", "date", "elderberry"]
specific_fruit = "banana"
is_present = specific_fruit in fruits_list
print("Task 4 - Is '{}' Present in Fruits List: {}".format(specific_fruit, is_present))

# Task 5: Iterate through a list of words and print each word in uppercase.
words_list = ["apple", "banana", "cherry", "date", "elderberry"]
uppercase_words = [word.upper() for word in words_list]
print("Task 5 - Uppercase Words List:", uppercase_words)

# Task 6: Write a function to reverse a list without using the reverse method.
def reverse_list(input_list):
    return input_list[::-1]

original_list = [1, 2, 3, 4, 5]
reversed_list = reverse_list(original_list)
print("Task 6 - Reversed List:", reversed_list)

# Task 7: Given a list of numbers, find the maximum and minimum values.
numbers_list = [10, 20, 30, 40, 50]
max_value = max(numbers_list)
min_value = min(numbers_list)
print("Task 7 - Max Value:", max_value)
print("Task 7 - Min Value:", min_value)

# Task 8: Remove the first occurrence of a specific element from a list.
fruits_list = ["apple", "banana", "cherry", "date", "elderberry"]
element_to_remove = "cherry"
if element_to_remove in fruits_list:
    fruits_list.remove(element_to_remove)
print("Task 8 - Updated Fruits List:", fruits_list)

# Task 9: Create a list of names and sort them in alphabetical order.
names_list = ["Charlie", "Alice", "Bob", "David"]
sorted_names = sorted(names_list)
print("Task 9 - Sorted Names List:", sorted_names)

# Task 10: Count the occurrences of a specific element in a list.
numbers_list = [1, 2, 2, 3, 4, 4, 5]
specific_element = 2
count_occurrences = numbers_list.count(specific_element)
print("Task 10 - Count of '{}' in Numbers List:".format(specific_element), count_occurrences)

# Task 11: Merge two lists into a new list without duplicates.
list1 = [1, 2, 3]
list2 = [3, 4, 5]
merged_list = list(set(list1 + list2))
print("Task 11 - Merged List without Duplicates:", merged_list)

# Task 12: Write a function to check if a list is a palindrome.
def is_palindrome(input_list):
    return input_list == input_list[::-1]

palindrome_list = [1, 2, 3, 2, 1]
not_palindrome_list = [1, 2, 3, 4, 5]
is_palindrome_result = is_palindrome(palindrome_list)
is_not_palindrome_result = is_palindrome(not_palindrome_list)
print("Task 12 - Is Palindrome:", is_palindrome_result)
print("Task 12 - Is Not Palindrome:", is_not_palindrome_result)

# Task 13: Given a list of strings, concatenate them into a single string.
strings_list = ["Hello", "World", "!"]
concatenated_string = ' '.join(strings_list)
print("Task 13 - Concatenated String:", concatenated_string)

# Task 14: Find the index of the first occurrence of a specific element in a list.
numbers_list = [10, 20, 30, 40, 50]
specific_element = 30
index = numbers_list.index(specific_element)
print("Task 14 - Index of {}: {}".format(specific_element, index))

# Task 15: Create a list of numbers and filter out the even numbers.
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = [num for num in numbers_list if num % 2 == 0]
print("Task 15 - Even Numbers List:", even_numbers)
Enter fullscreen mode Exit fullscreen mode
# Task 16: Write a function to remove all occurrences of a specific element from a list.
def remove_all_occurrences(input_list, element_to_remove):
    return [item for item in input_list if item != element_to_remove]

numbers_list = [1, 2, 2, 3, 4, 2, 5]
element_to_remove = 2
filtered_list = remove_all_occurrences(numbers_list, element_to_remove)
print("Task 16 - List with '{}' Removed:".format(element_to_remove), filtered_list)

# Task 17: Calculate the average of elements in a list of numbers.
def calculate_average(input_list):
    return sum(input_list) / len(input_list)

numbers_list = [10, 20, 30, 40, 50]
average = calculate_average(numbers_list)
print("Task 17 - Average of Elements in Numbers List:", average)

# Task 18: Create a list of strings and sort them by length.
strings_list = ["apple", "banana", "cherry", "date", "elderberry"]
sorted_by_length = sorted(strings_list, key=len)
print("Task 18 - Strings List Sorted by Length:", sorted_by_length)

# Task 19: Write a function to flatten a nested list into a single list.
def flatten_nested_list(nested_list):
    return [item for sublist in nested_list for item in sublist]

nested_list = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = flatten_nested_list(nested_list)
print("Task 19 - Flattened Nested List:", flattened_list)

# Task 20: Given a list of words, create a new list with the lengths of the words.
words_list = ["apple", "banana", "cherry", "date", "elderberry"]
word_lengths = [len(word) for word in words_list]
print("Task 20 - Word Lengths List:", word_lengths)

# Task 21: Find the second largest element in a list of numbers.
def find_second_largest(input_list):
    unique_sorted = sorted(set(input_list), reverse=True)
    if len(unique_sorted) >= 2:
        return unique_sorted[1]
    else:
        return None

numbers_list = [10, 20, 30, 40, 50]
second_largest = find_second_largest(numbers_list)
print("Task 21 - Second Largest Element:", second_largest)

# Task 22: Create a list of integers and check if all elements are greater than a specified value.
def are_all_greater(input_list, value):
    return all(item > value for item in input_list)

numbers_list = [10, 20, 30, 40, 50]
specified_value = 5
all_greater = are_all_greater(numbers_list, specified_value)
print("Task 22 - Are All Elements Greater than {}:".format(specified_value), all_greater)

# Task 23: Write a function to rotate a list by a specified number of positions.
def rotate_list(input_list, positions):
    return input_list[-positions:] + input_list[:-positions]

original_list = [1, 2, 3, 4, 5]
rotated_list = rotate_list(original_list, 2)
print("Task 23 - Rotated List:", rotated_list)

# Task 24: Remove duplicate elements from a list while preserving the order.
def remove_duplicates_preserve_order(input_list):
    seen = set()
    result = []
    for item in input_list:
        if item not in seen:
            seen.add(item)
            result.append(item)
    return result

numbers_list = [1, 2, 2, 3, 4, 4, 5]
no_duplicates_list = remove_duplicates_preserve_order(numbers_list)
print("Task 24 - List with Duplicates Removed (Preserving Order):", no_duplicates_list)

# Task 25: Given two lists of numbers, find the common elements between them.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_elements = list(set(list1).intersection(list2))
print("Task 25 - Common Elements Between List1 and List2:", common_elements)

# Task 26: Write a function to split a list into sublists of a specified size.
def split_list(input_list, sublist_size):
    return [input_list[i:i + sublist_size] for i in range(0, len(input_list), sublist_size)]

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sublist_size = 3
sublists = split_list(original_list, sublist_size)
print("Task 26 - Sublists of Size {}:".format(sublist_size), sublists)

# Task 27: Shuffle the elements of a list randomly.
import random

numbers_list = [1, 2, 3, 4, 5]
random.shuffle(numbers_list)
print("Task 27
# Task 27 - Shuffled List:", numbers_list

# Task 28: Create a list of strings and capitalize the first letter of each string.
strings_list = ["apple", "banana", "cherry", "date", "elderberry"]
capitalized_strings = [word.capitalize() for word in strings_list]
print("Task 28 - Capitalized Strings List:", capitalized_strings)

# Task 29: Write a function to find the intersection of multiple lists.
def intersection_of_lists(*lists):
    return list(set.intersection(*map(set, lists)))

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
list3 = [5, 6, 7, 8, 9]
common_elements = intersection_of_lists(list1, list2, list3)
print("Task 29 - Common Elements Among Lists:", common_elements)

# Task 30: Given a list of numbers, calculate the product of all the elements.
def calculate_product(input_list):
    product = 1
    for item in input_list:
        product *= item
    return product

numbers_list = [1, 2, 3, 4, 5]
product = calculate_product(numbers_list)
print("Task 30 - Product of Elements in Numbers List:", product)
Enter fullscreen mode Exit fullscreen mode

Find the second largest element in a list of numbers

data = [4, 6, 9, 10]
maxdata = max(data)
secondata = 0

for newdata in data:
    if secondata < newdata < maxdata:
        secondata = newdata

print(secondata)
Enter fullscreen mode Exit fullscreen mode
Task 16 - List with '2' Removed: [1, 3, 4, 5]
Task 17 - Average of Elements in Numbers List: 30.0
Task 18 - Strings List Sorted by Length: ['date', 'apple', 'banana', 'cherry', 'elderberry']
Task 19 - Flattened Nested List: [1, 2, 3, 4, 5, 6, 7, 8]
Task 20 - Word Lengths List: [5, 6, 6, 4, 11]
Task 21 - Second Largest Element: 40
Task 22 - Are All Elements Greater than 5: True
Task 23 - Rotated List: [4, 5, 1, 2, 3]
Task 24 - List with Duplicates Removed (Preserving Order): [1, 2, 3, 4, 5]
Task 25 - Common Elements Between List1 and List2: [4, 5]
Task 26 - Sublists of Size 3: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Task 27 - Shuffled List: [4, 3, 2, 1, 5]
Task 28 - Capitalized Strings List: ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']
Task 29 - Common Elements Among Lists: [5]
Task 30 - Product of Elements in Numbers List: 120
Enter fullscreen mode Exit fullscreen mode

Good question with list

Append a string to an existing list of names.
Write a function to remove duplicates from a list using a set.
Iterate through a list of words and print each word in uppercase
Remove the first occurrence of a specific element from a list
Write a function to check if a list is a palindrome.
Write a function to remove all occurrences of a specific element from a list.
Given a list of words, create a new list with the lengths of the words.
Find the second largest element in a list of numbers

Top comments (0)