[Link].
in
Python Lists
1. Introduction to Lists
• A list is a collection that is:
o Ordered
o Mutable (changeable)
o Allows duplicate values
• Lists are written using square brackets [ ].
• List elements can be of different data types.
• Lists can be created by placing comma-separated values inside square
brackets.
2. List Index
• Lists are indexed starting from 0.
• Negative indexing starts from -1 (last element).
3. Accessing Single Element from List
• Access individual elements using their index.
• Syntax: list[index]
4. Accessing Multiple Elements from List (Slicing)
1
[Link]
• Slicing allows you to extract a portion of a list using a specific range of
indices.
• Syntax: list[start : end : step]
o start: index to begin the slice
o end: index to stop (excluded)
o step: how many steps to skip
Default values based on step:
• If step is positive:
o start defaults to 0
o end defaults to end of list
• If step is negative:
o start defaults to -1 (last element)
o end defaults to beginning of list
• Examples
1. Slice from index 1 to 4 (excluding 4)
2. Slice from beginning to index 3 (excluding 3)
3. Slice from index 3 to end
2
[Link]
4. Slice full list
5. Slice with step = 2
6. Reverse list using slicing
7. Slice with negative step from end to start
8. Slice from 2nd to last to 2nd element
3
[Link]
9. Slice last 3 elements
10. Slice first 3 elements using negative index
11. Skip every third element
5. Check Item Using in and not in Keywords
• Use in to check if an item exists in the list.
• Use not in to check if an item does not exist.
4
[Link]
6. List Concatenation and Multiplication
• Use + to join two lists.
• Use * to repeat a list multiple times.
7. List Length
• Use len() to find how many elements are in a list.
8. Change List Item
• You can modify a specific element in the list using its index.
9. Change a Range of Item Values
• You can change multiple values at once using slicing.
• The new values can be more or fewer than the replaced ones.
Example 1: Replace two items with two new values
5
[Link]
Example 2: Replace one item with three new values
Example 3: Replace three items with one value
Note: The length of the list changes if the number of inserted items differs from
the number of replaced items.
10. List Unpacking
• Unpacking allows you to assign individual elements of a list to variables
in one line.
• The number of variables on the left must match the number of elements in
the list.
6
[Link]
11. Extended Unpacking using Asterisk (*)
• Introduced in Python 3.x for unpacking iterables in a flexible way.
• Allows you to capture multiple remaining elements using a * (starred)
variable.
• You can use it in any position (start, middle, or end) of the unpacking line.
• Only one starred variable is allowed during unpacking.
• The starred variable will always be a list, even if it gets zero elements.
Example 1: Starred variable at the end
Example 2: Starred variable at the beginning
Example 3: Starred variable in the middle
7
[Link]
10. List Constructor
• You can create a list from another iterable (like a tuple, string, set,
dictionary) using the list() constructor.
1. Tuple → List
2. String → List
3. Set → List
4. Dictionary → List
Only the keys are included in the resulting list.
5. Range → List