Lists and Their Magical Powers โ
Explore how superheroes manage teams and inventories using lists in Python.
Python's List Magic Show ๐ฉโจ โ
Lists are like teams of superheroesโdynamic and powerful.
The Shape-Shifting Lists ๐ โ
Lists can change, just like a superhero adapting to challenges.
team = ["Iron Man", "Captain America", "Thor"]
team.append("Hulk") # Add a new member
team.remove("Iron Man") # Member leaves
print(team)
# Output: ['Captain America', 'Thor', 'Hulk']
Question: How can we add multiple members at once?
Example:
new_members = ["Black Widow", "Hawkeye"]
team.extend(new_members)
print(team)
# Output: ['Captain America', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye']
Elemental Conjuring and Banishment ๐๐ซ โ
Summon and dismiss elements in a list.
inventory = ["Shield", "Armor"]
inventory.insert(1, "Helmet") # Add at specific position
lost_item = inventory.pop(0) # Remove by position
print(f"Inventory: {inventory}, Lost: {lost_item}")
# Output: Inventory: ['Helmet', 'Armor'], Lost: Shield
Task
Replace "Armor" with "Nano Suit" in the inventory.
Answer
index = inventory.index("Armor")
inventory[index] = "Nano Suit"
print(inventory)
# Output: ['Helmet', 'Nano Suit']
The Alchemy of Lists: Merging and Cloning ๐ฎ โ
Merging Teams โ
team_a = ["Spider-Man", "Doctor Strange"]
team_b = ["Black Panther", "Captain Marvel"]
united_team = team_a + team_b
print(united_team)
# Output: ['Spider-Man', 'Doctor Strange', 'Black Panther', 'Captain Marvel']
Question: How can we sort the united team alphabetically?
Answer
united_team.sort()
print(united_team)
# Output: ['Black Panther', 'Captain Marvel', 'Doctor Strange', 'Spider-Man']
Cloning Gadgets โ
gadgets = ["Web Shooter"] * 3
print(gadgets)
# Output: ['Web Shooter', 'Web Shooter', 'Web Shooter']
Arcane Copies and Mystical Sorts ๐ โ
Shadow Copying โ
original = ["Thor's Hammer", "Captain's Shield"]
copy = original[:]
copy.append("Iron Man's Suit")
print(f"Original: {original}, Copy: {copy}")
# Output: Original: ["Thor's Hammer", "Captain's Shield"], Copy: ["Thor's Hammer", "Captain's Shield", "Iron Man's Suit"]
Question: What happens if we modify the original list after copying?
Example:
original.remove("Thor's Hammer")
print(f"Original: {original}, Copy: {copy}")
# Output: Original: ["Captain's Shield"], Copy: ["Thor's Hammer", "Captain's Shield", "Iron Man's Suit"]
The Alchemy of List Manipulation ๐โจ โ
Dive into transforming lists, from doubling powers to orchestrating order.
Doubling Strength โ
# Amplifying power
player_stats = [10, 20, 30]
for i in range(len(player_stats)):
player_stats[i] *= 2
print("Amplified Stats:", player_stats)
# Output: Amplified Stats: [20, 40, 60]
Advanced List Comprehension ๐ โ
Efficiently process lists.
Character Count in Names โ
heroes = ["Batman", "Superman", "Wonder Woman"]
name_lengths = [len(hero) for hero in heroes]
print(name_lengths)
# Output: [6, 8, 12]
Filtering Heroes โ
long_named_heroes = [hero for hero in heroes if len(hero) > 6]
print(long_named_heroes)
# Output: ['Superman', 'Wonder Woman']
Question: How can we create a list of heroes' names in uppercase?
Answer
uppercase_heroes = [hero.upper() for hero in heroes]
print(uppercase_heroes)
# Output: ['BATMAN', 'SUPERMAN', 'WONDER WOMAN']
Conclusion โ
By mastering lists, you've learned how to manage collections of items effectively. Whether it's assembling a team of heroes or managing an inventory of gadgets, lists are essential tools in your Python journey.