Python List Operations and Slicing

Python Lists 🐍 Lists are: • Mutable (changeable) • Ordered • Allow duplicates Created using [] List Slicing : Slicing lets you get subsets of the list. Syntax: list[start:stop] (stop is exclusive). You can omit start/stop or use negative indices. Adding Items to Lists: append(item): Adds to the end. insert(index, item): Inserts at a specific position. extend(iterable): Adds multiple items from another iterable (better than appending a list, which would nest it). Removing Items from Lists: remove(value): Removes the first occurrence of a value. pop(): Removes and returns the last item (or from a specific index with pop(index)). 📝 Python Lists - Example print("----- Creating List -----") Topics = ["AWS","GitHub","Linux","Terraform","Kubernetes"] print("Topics:", Topics) print("Length:", len(Topics)) print("First Item:", Topics[0]) print("4th Item:", Topics[3]) print("Last Item:", Topics[-1]) print("Second Last Item:", Topics[-2]) print("\n----- Slicing -----") print("Topics[0:2]:", Topics[0:2]) print("Topics[:2]:", Topics[:2]) print("Topics[2:]:", Topics[2:]) print("\n----- Adding Items -----") Topics.append('GCP') print("After append:", Topics) Topics.insert(0,'CICD') print("After insert:", Topics) Topics2 = ['Python','Go'] Topics.extend(Topics2) print("After extend:", Topics) print("\n----- Removing Items -----") Topics.remove('AWS') print("After remove AWS:", Topics) popped = Topics.pop() print("Popped Item:", popped) print("After pop:", Topics) #Python

  • text

To view or add a comment, sign in

Explore content categories