Q1). What is a Python list?
A Python list is a collection of items that are ordered, mutable (changeable), and allow duplicate elements. Lists are created using square brackets.
For example: `my_list = [1, 2, 3, 4]`. This means you can have a list of items like numbers, strings, or even other lists.
Q2). How do you create a list in Python?
You create a list by placing elements inside square brackets, separated by commas. For example: `my_list = [10, 20, 30]`. This creates a list with three elements: 10, 20, and 30.
Q3). How can you access elements in a list?
You access elements in a list using indexing. The index starts at 0.
For example: if `my_list = [10, 20, 30]`, then `my_list[1]` gives you `20`, which is the element at index 1.
Q4). How do you add an element to a list?
You can add an element to a list using the `append()` method.
For example: if `my_list = [1, 2, 3]`, then `my_list.append(4)` will change `my_list` to `[1, 2, 3, 4]`. This is like adding a new item to the end of a shopping list.
Q5). How do you remove an element from a list?
You can remove an element using the `remove()` method.
For example: if `my_list = [1, 2, 3, 4]` and you call `my_list.remove(2)`, it will remove the value `2`, leaving `[1, 3, 4]`. This is like crossing out an item from your to-do list.
Q6). What is list slicing?
List slicing allows you to get a subset of a list. You use the `start:stop` syntax.
For example: `my_list[1:3]` gives you elements from index 1 to 2. If `my_list = [10, 20, 30, 40]`, then `my_list[1:3]` gives `[20, 30]`, which is like picking a portion of a list.
Q7). How do you find the length of a list?
You find the length of a list using the `len()` function.
For example: `len(my_list)` will give you the number of elements in `my_list`. If `my_list = [10, 20, 30]`, `len(my_list)` returns `3`.
Q8). What is list comprehension?
List comprehension is a concise way to create lists. It uses a single line of code.
For example: `[x**2 for x in range(5)]` creates a list of squares of numbers from 0 to 4: `[0, 1, 4, 9, 16]`. It’s like creating a list of squared numbers in one line.
Q9). How do you sort a list?
You can sort a list using the `sort()` method or `sorted()` function. `my_list.sort()` sorts the list in place, while `sorted(my_list)` returns a new sorted list.
For example: if `my_list = [3, 1, 2]`, then `my_list.sort()` will change `my_list` to `[1, 2, 3]`.
Q10). What is the difference between `sort()` and `sorted()`?
`sort()` sorts the list in place and returns `None`, while `sorted()` returns a new sorted list without changing the original list.
For example: `sorted([3, 1, 2])` returns `[1, 2, 3]` but does not alter the original list `[3, 1, 2]`.
Q11). How do you reverse a list?
You can reverse a list using the `reverse()` method or slicing. `my_list.reverse()` reverses the list in place, while `my_list[::-1]` creates a new reversed list.
For example: if `my_list = [1, 2, 3]`, `my_list.reverse()` will change it to `[3, 2, 1]`.
Q12). How do you check if an element is in a list?
You can check if an element is in a list using the `in` keyword.
For example: `3 in my_list` will return `True` if `3` is in `my_list`. If `my_list = [1, 2, 3]`, `3 in my_list` returns `True`.
Q13). How do you concatenate two lists?
You can concatenate two lists using the `+` operator.
For example: `[1, 2] + [3, 4]` results in `[1, 2, 3, 4]`. This is like merging two separate lists into one.
Q14). What is the `extend()` method?
The `extend()` method adds elements from another iterable (like a list) to the end of the list.
For example: if `my_list = [1, 2]`, then `my_list.extend([3, 4])` adds `3` and `4` to `my_list`, making it `[1, 2, 3, 4]`.
Q15). How do you copy a list?
You can copy a list using slicing `my_list[:]` or the `copy()` method.
For example: `my_list.copy()` creates a shallow copy of the list. If `my_list = [1, 2, 3]`, `my_list.copy()` returns a new list `[1, 2, 3]`.
Q16). What are nested lists?
Nested lists are lists within lists.
For example: `nested_list = [[1, 2], [3, 4]]` is a list with two lists inside it. To access elements, use multiple indices, e.g., `nested_list[0][1]` gives `2`.
Q17). How do you find the index of an element?
You can find the index of an element using the `index()` method.
For example: `my_list.index(20)` returns the index of `20` in `my_list`. If `my_list = [10, 20, 30]`, `my_list.index(20)` returns `1`.
Q18). How do you remove an element by index?
You can remove an element by index using the `pop()` method.
For example: `my_list.pop(2)` removes the element at index `2` and returns it. If `my_list = [10, 20, 30, 40]`, `my_list.pop(2)` returns `30`.
Q19). What does `list.count(value)` do?
`list.count(value)` returns the number of occurrences of `value` in the list.
For example: `[1, 2, 2, 3].count(2)` returns `2`, as `2` appears twice.
Q20). How can you find all occurrences of an element in a list?
To find all occurrences, use a loop with `enumerate()` or list comprehension.
For example: `[index for index, value in enumerate(my_list) if value == 2]` finds all indices where `2` occurs in `my_list`.
Q21). What is the `del` statement used for?
`del` is used to delete an element by index or delete the entire list.
For example: `del my_list[1]` removes the element at index `1`, while `del my_list` removes the whole list.
Q22). How do you create a list with repeated elements?
You can create a list with repeated elements using multiplication.
For example: `[0] * 5` creates `[0, 0, 0, 0, 0]`, which is like initializing a list with five zeros.
Q23). How do you check the type of an object?
You can check the type of an object using the `type()` function.
For example: `type(my_list)` will return `
` for a list. It helps you understand what kind of object you're working with.
Q24). How do you get a sublist from a list?
You can get a sublist using slicing.
For example: `my_list[1:4]` gives a sublist from index `1` to `3`. If `my_list = [10, 20, 30, 40, 50]`, then `my_list[1:4]` returns `[20, 30, 40]`.
Q25). How do you insert an element at a specific position?
You can insert an element using the `insert()` method.
For example: `my_list.insert(1, 15)` inserts `15` at index `1`. If `my_list = [10, 20, 30]`, it becomes `[10, 15, 20, 30]`.
Q26). What is a list comprehension?
List comprehension allows you to create lists in a single line.
For example: `[x**2 for x in range(5)]` creates `[0, 1, 4, 9, 16]` by squaring each number from `0` to `4`.
Q27). How do you flatten a 2D list?
You can flatten a 2D list using list comprehension.
For example: `flattened = [item for sublist in matrix for item in sublist]` converts `matrix = [[1, 2], [3, 4]]` to `[1, 2, 3, 4]`.
Q28). How do you remove duplicates from a list?
To remove duplicates, convert the list to a set and back to a list.
For example: `list(set(my_list))` removes duplicates. If `my_list = [1, 2, 2, 3]`, `list(set(my_list))` returns `[1, 2, 3]`.
Q29). What are list slicing techniques?
Techniques include extracting parts of lists, reversing lists, and stepping through elements.
For example: `my_list[1:3]` extracts elements from index `1` to `2`, and `my_list[::-1]` reverses the list.
Q30). How do you merge multiple lists into one list?
You can merge multiple lists using the `+` operator or `extend()`.
For example: `[1, 2] + [3, 4]` merges lists into `[1, 2, 3, 4]`, or `my_list.extend([5, 6])` adds `[5, 6]` to the end of `my_list`.
Q31). What is a circular list?
A circular list is a concept where the end of the list points back to the start, forming a circle. Python doesn’t have built-in support, but you can simulate it by using list indexing to loop back to the start.
Q32). How do you handle nested lists in functions?
Handle nested lists by passing them as arguments and iterating through them. Use recursion for deeply nested structures.
For example: a function that sums all numbers in a nested list might call itself for each sublist.
Q33). What is the `list.copy()` method used for?
The `list.copy()` method creates a shallow copy of the list. This means changes to the copy do not affect the original list.
For example: `my_list.copy()` creates a new list with the same elements as `my_list`, but modifying it won’t change `my_list`.