Python-Unit 2
~Tamanna Shah
Python List Slicing
• Python list slicing is fundamental concept that let us easily access specific
elements in a list. In this article, we’ll learn the syntax and how to use both
positive and negative indexing for slicing with examples.
• Example: Get the items from a list starting at position 1 and ending at
position 4 (exclusive).
• a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
•
• # Get elements from index 1 to 4 (excluded)
• print(a[1:4])
Out-of-bound slicing
• In Python, list slicing allows out-of-bound indexing without raising
errors. If we specify indices beyond the list length then it will simply
return the available items.
• Example: The slice a[7:15] starts at index 7 and attempts to reach
index 15, but since the list ends at index 8, so it will return only the
available elements (i.e. [8,9]).
Negative Indexing
• Negative indexing is useful for accessing elements from the end of
the list. The last element has an index of -1, the second last element -
2, and so on.
Merging 2 lists
• + operator
Merging 2 lists
• Extend()
Python List built in methods
• Python List methods – GeeksforGeeks
• Tuples
• [Link]
• Dictonary
• [Link]
• [Link]