The document provides an overview of list manipulation in Python, covering definitions, creation, and various functions related to lists. Key topics include list types, indexing, slicing, and methods such as append, extend, pop, and remove. It also highlights differences between lists and strings, as well as operations like concatenation and replication.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
Ch-11[List Manipulation]
The document provides an overview of list manipulation in Python, covering definitions, creation, and various functions related to lists. Key topics include list types, indexing, slicing, and methods such as append, extend, pop, and remove. It also highlights differences between lists and strings, as well as operations like concatenation and replication.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Unit-I[Computer System Organisation]
Ch-11[List Manipulation]
1. What are lists?
Ans. Lists in Python are ordered collections of items that can hold elements of different data types. They are created using square brackets [] and are mutable, meaning elements can be added, removed, or changed. For example: my_list = [1, "hello", 3.14]. 2. How are lists created? Ans. Lists in Python are created by placing comma- separated values within square brackets []. For example: my_list = [1, 2, 3]. You can also create an empty list using empty_list = []. 3. What is an empty list? Ans. An empty list in Python is a list with no elements. It is created using empty square brackets []. For example: empty_list = []. 4. What is a long list? Ans. In Python, a long list refers to a list containing a large number of elements. There is no fixed number that defines a "long" list; it's relative to the context of use. For example: long_list = [1, 2, 3, ..., 1000] could be considered long compared to a shorter list like [1, 2, 3]. 5. What is a nested list? Ans. A nested list in Python is a list that contains other lists as its elements. This allows for creating multi-dimensional lists. For example: nested_list = [[1, 2], [3, 4], [5, 6]]. 6. How can a list be being accessed? Ans. A list in Python can be accessed by referring to its index using square brackets []. The index starts at 0 for the first element. For example, my_list[0]
accesses the first element of my_list. Negative
indices can also be used to access elements from the end of the list, like my_list[-1] for the last element. 7. Describe length function in lists. Ans. The len() function in Python is used to find the number of elements in a list. It returns an integer representing the total count of items. For example, len(my_list) would return the number of elements in my_list. 8. Describe indexing and slicing in lists. Ans. Indexing in lists allows access to individual elements using their position in the list. The index is placed within square brackets [], starting from 0 for the first element. For example, my_list[0] accesses the first element. Slicing in lists enables access to a sublist by specifying a range of indices. The syntax is list[start:stop:step], where start is the index to begin, stop is the index to end (not inclusive), and step is the interval between elements. For example, my_list[1:4] gets elements from index 1 to 3. 9. Describe membership operator in lists. Ans. The membership operators in and not in are used to test whether an element exists within a list. The in operator returns True if the element is found in the list, otherwise False. The not in operator returns True if the element is not found in the list, otherwise False 10. Describe concatenation and replication operators in lists. Ans. Concatenation: The + operator is used to concatenate, or join, two lists into one. For example, list1 + list2 results in a new list containing elements from both list1 and list2. Replication: The * operator is used to replicate, or repeat, a list a specified number of times. For example, list1 * 3 results in a new list with elements of list1 repeated three times.
11. How are a list being different from a
string? Ans. Lists and strings differ in several key ways: Data types: Lists can contain elements of different data types (e.g., integers, strings, floats), while strings contain only characters. Mutability: Lists are mutable, meaning their elements can be changed. Strings are immutable, so their characters cannot be altered. 12. Describe traversing a list. Ans. Traversing a list in Python involves accessing each element of the list one by one, typically using a loop. The most common way is to use a for loop. 13. How are two lists compared? Ans. Two lists in Python are compared element by element using comparison operators like ==, !=, <, >, <=, and >=. The comparison starts with the first element of each list and proceeds element by element. 14. How are two list joined? Ans. Two lists in Python can be joined using the + operator. This concatenates the two lists into a single list. 15. How are two list repeated or replicated? Ans. Two lists in Python can be replicated using the * operator. This repeats the elements of the list a specified number of times. 16. How are lists being sliced? Ans. Lists in Python are sliced using the colon : within square brackets []. The syntax is list[start:stop:step], where start is the index to begin, stop is the index to end (not inclusive), and step is the interval between elements. For example, my_list[1:4] gets elements from index 1 to 3. 17. How to create a true copy of a list? Ans. A true copy of a list in Python can be created using the copy() method or by slicing the entire list. Both methods create a new list that is an exact copy of the original. 18. Define len() function. Ans. The len() function in Python is used to determine the number of elements in a sequence, such as a list, string, or tuple. It returns an integer representing the total count of items. For example, len(my_list) returns the number of elements in my_list. 19. Define list() function. Ans. The list() function in Python is used to create a new list. It can convert other iterable data types (such as strings, tuples, or sets) into a list. 20. Define index () function. Ans. The index() function in Python is used to find the first occurrence of a specified value within a list. It returns the index of the value if it is found. If the value is not found, it raises a ValueError. 21. Define append () function. Ans. The append() function in Python is used to add a single element to the end of a list. This function modifies the original list in place. 22. Define extend () function. Ans. The extend() function in Python is used to add multiple elements to the end of a list. Unlike append(), which adds a single element, extend() takes an iterable (like another list) and adds each of its elements to the list.
23. Define pop() function.
Ans. The pop() function in Python removes and returns the last element of a list by default. It modifies the original list. You can also specify an index to remove and return an element at that position. 24. Define remove() function. Ans. The remove() function in Python is used to remove the first occurrence of a specified element from a list. If the element is not found, it raises a ValueError. 25. Define clear() function. Ans. The clear() function in Python is used to remove all elements from a list, resulting in an empty list. It modifies the original list in place. 26. Define count() function. Ans. The count() function in Python is used to count the number of occurrences of a specified value within a list. It returns an integer representing the count. 27. Define reverse() function. Ans. The reverse() function in Python is used to reverse the order of elements in a list. It modifies the original list in place. 28. Define sort() function. Ans. The sort() function in Python is used to sort the elements of a list in ascending order by default. It modifies the original list in place. You can also specify the sorting order by using the reverse parameter. In this case, the sort() function sorts the elements of my_list in ascending order, and with reverse=True, it sorts in descending order. 29. Define sorted() function. Ans. The sorted() function in Python is used to create a new sorted list from an iterable, leaving the original list unchanged. It returns a new list with elements sorted in ascending order by default. You can specify the sorting order by using the reverse parameter. 30. Define max() function. Ans. The max() function in Python is used to find the largest element in an iterable, such as a list, tuple, or set. It returns the maximum value. 31. Define min() function. Ans. The min() function in Python is used to find the smallest element in an iterable, such as a list, tuple, or set. It returns the minimum value. 32. Define sum() function. Ans. The sum() function in Python is used to calculate the total sum of all elements in an iterable, such as a list or tuple. It returns the sum as an integer or float. 33. How to append a single element in list? Ans. To append a single element to a list in Python, you use the append() method. This method adds the specified element to the end of the list. 34. How to append a list of elements in list? Ans. To append a list of elements to another list in Python, you can use the extend() method. This method adds each element of the specified list to the end of the original list. 35. How to insert a single element in list? Ans. To insert a single element into a list at a specific position in Python, you use the insert() method. This method takes two arguments: the index at which to insert the element and the element itself. 36. How to modify/update element to list? Ans. To modify or update an element in a list in Python, you can assign a new value to a specific index. This directly changes the element at that position. 37. How to delete an element using index? Ans. To delete an element from a list using its index in Python, you can use the del statement or the pop() method. Both methods remove the element at the specified index. 38. How to delete an element using its value? Ans. To delete an element from a list using its value in Python, you can use the remove() function. This function removes the first occurrence of the specified value from the list. 39. How to delete a sub list from list? Ans. To delete a sublist (a specific portion) from a list in Python, you can use slicing with the del statement. This allows you to specify the start and end indices of the sublist you want to remove.