Debug School

rakesh kumar
rakesh kumar

Posted on

How to get new list from existing list where new list first elements starts with existing list third element in django

If you want to start with the third element of the given list and retrieve a new list containing only those elements, you can modify the code as follows:

data = ['tikka', '', 'Calories: 200', 'Fat: 11g', 'Carbohydrates: 8g', 'Protein: 15g', 'Sodium: 250mg', 'Fiber: 0g', 'Sugar: 6g']
Enter fullscreen mode Exit fullscreen mode
# Start with the third element of the list
data = data[2:]

# Create a new list to store the desired elements
desired_list = []

# Iterate over the data
for item in data:
    # Check if the item starts with a colon (':')
    if item.startswith(':'):
        # Remove the colon and any leading/trailing whitespaces from the item
        item = item[1:].strip()

        # Append the modified item to the desired list
        desired_list.append(item)

# Print the desired list
print(desired_list)
Enter fullscreen mode Exit fullscreen mode

Output:

['Calories: 200', 'Fat: 11g', 'Carbohydrates: 8g', 'Protein: 15g', 'Sodium: 250mg', 'Fiber: 0g', 'Sugar: 6g']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)