0% found this document useful (0 votes)
8 views1 page

Python List Assignment Instructions

Uploaded by

kalisubrata609
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)
8 views1 page

Python List Assignment Instructions

Uploaded by

kalisubrata609
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

List Assignment

Instructions: Write answers to these questions in jupyter notebook and submit the
assignment in this drive upload_link by naming it as
“python_list_assignment_batch_name_your_name.ipynb”

1. Create a list named `my_list` containing the integers 1, 2, and 3.


2. Print the second element of `my_list`.
3. Use list slicing to print the sublist [2, 3] from `my_list`.
4. Concatenate `my_list` with another list [4, 5, 6] and print the result.
5. Create a list of strings: `colors = ['red', 'blue', 'green']`. Append the string 'yellow' to the list
and print the updated list.
6. Create a list containing duplicate elements, such as `[1, 2, 2, 3, 3, 3]`. Count and print the
number of occurrences of the element 3.
7. Sort the list `[5, 3, 8, 1, 2]` in ascending order and print the sorted list.
8. Write a program that takes a list of numbers as input and calculates the sum of all
elements in the list.
9. Use list comprehension to create a list of squares of numbers from 1 to 5.
10. Reverse the list `[1, 2, 3, 4, 5]` using slicing.
11. Write a program that takes two lists as input and merges them into a single sorted list.
12. Remove the element 'green' from the list `colors = ['red', 'blue', 'green', 'yellow']` and print
the modified list.
13. Remove the last element from the list `[10, 20, 30, 40, 50]` and print the modified list.
14. Find the index of the element 25 in the list `[10, 15, 20, 25, 30]` and print the index.
15. Check if the list `[1, 2, 3]` is a subset of the list `[1, 2, 3, 4, 5]`.
16. Write a program that takes a list of words as input and returns a new list containing only
the words with more than 5 characters.
17. Find and print the maximum and minimum elements in the list `[10, 5, 20, 8, 15]`.
18. Join the elements of a list ['apple', 'banana', 'cherry'] into a single string separated by
commas.
19. Write a program that takes a list of numbers and removes all even numbers from the list.
20. Use list comprehension to create a list of tuples where each tuple contains an element
and its square, for numbers from 1 to 5.

Common questions

Powered by AI

Using `remove()`, which deletes elements by value, has an O(n) time complexity as it scans the list from the start until it finds the element. This can be inefficient with large lists with multiple matching values. In contrast, slicing operations to exclude specific elements, such as `colors[:1] + colors[2:]` to remove 'green', can allow for removing by index with known positions, but this approach increases complexity and might not be intuitive for all use cases .

The `sum()` function, an inherently optimized C-implemented function, should be preferred over manual loops for summing list elements in most scenarios due to its succinct syntax and performance efficiency on large lists. Manual loops could be justified if additional operations are desired within the loop, but this generally increases code complexity without significant benefits .

The `in` keyword checks for membership of elements, determining if an item is within a collection, crucial for subset operations like verifying if `[1, 2, 3]` is a subset of `[1, 2, 3, 4, 5]`. Each element's presence is checked, making the `all()` function with a generator expression a practical approach for subset checks, taking advantage of short-circuit evaluation while maintaining readability and conciseness .

List slicing in Python allows for concise and efficient retrieval of sublists. It provides a way to access portions of a list without needing explicit loops or additional functions, making code shorter and easier to understand. For example, slicing a list like `my_list = [1, 2, 3]` to get `[2, 3]` is done with `my_list[1:]`, which is both efficient and readable .

List comprehension allows for filtering by embedding conditional expressions within the comprehension. For example, filtering words longer than five characters can be achieved with `[word for word in words if len(word) > 5]`, where `if len(word) > 5` acts as a filter, efficiently generating results without separate loops or additional filtering functions .

Concatenating two lists in Python, such as `my_list + [4, 5, 6]`, creates a new list rather than altering the original lists. This means additional memory is used to store the new object, which can impact performance if working with large datasets. Thus, while simple for small lists, this operation can become costly in terms of memory usage and runtime during concatenation of large lists .

One common strategy to remove duplicates is converting the list to a set and back to a list, like in `list(set(my_list))`, ensuring all elements are unique. However, this approach does not preserve the original order. To maintain order, you can use a list comprehension with a condition to ensure each element is appended only once, or use OrderedDict from the collections module before Python 3.7 .

The `sort()` method in Python is implemented using Timsort, which has an average and worst-case time complexity of O(n log n). This efficiency makes it suitable for sorting both small and large datasets. However, its performance can degrade with very large lists due to memory overhead, and its in-place nature means the original list order is altered, which might require copying if preservation is necessary .

List comprehension is more concise and often faster than traditional for loops, as it allows for inline expressions and is optimized for performance. For creating a list of squares from 1 to 5, the syntax `[x**2 for x in range(1, 6)]` clearly conveys the intent in a single line, whereas using a for loop would require multiple lines to initialize lists, iterate, and append results .

The `append()` method adds a single element to the end of a list, modifying the list in place without creating a new list object. This operation is O(1) on average due to amortized costs when dynamic resizing occurs but can affect memory allocation patterns. It is useful for growing lists efficiently within loop structures or iterative list building scenarios .

You might also like