DEV Community

Saman Sardari
Saman Sardari

Posted on

Finding Active Socks

Of course! Here’s the code with a headline included:

# Finding Active Socks

# Define a list of socks with their status
socks = [
    {"id": 1, "active": True},
    {"id": 2, "active": False},
    {"id": 3, "active": True},
    {"id": 4, "active": False}
]

# Function to find active socks
def find_active_socks(socks):
    active_socks = [sock for sock in socks if sock["active"]]
    return active_socks

# Find and print active socks
active_socks = find_active_socks(socks)
print("Active Socks:")
for sock in active_socks:
    print(f"Sock ID: {sock['id']}")
Enter fullscreen mode Exit fullscreen mode

Title: Finding Active Socks

Explanation:

  1. Define Data: A list named socks is created where each sock is represented as a dictionary with an "id" and "active" status.
  2. Function to Find Active Socks: The find_active_socks function filters the list to return only the socks that are active.
  3. Print Active Socks: The code then prints out the IDs of the active socks using a for loop.

This script identifies and displays the socks that are currently active based on their status.
the author:دوربین سیم کارت خور-دوربین کوچک

Top comments (0)