Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Logic2apply/13209861355dc7466d9ce72fc81a908e to your computer and use it in GitHub Desktop.
Save Logic2apply/13209861355dc7466d9ce72fc81a908e to your computer and use it in GitHub Desktop.
You visited a restaurant called EXAMPLE, and the food items in that restaurant are sorted, based on their amount of calories. You have to reserve this list of food items containing calories.

Food and Calories

Problem Statement:-

You visited a restaurant called CodeWithHarry, and the food items in that restaurant are sorted, based on their amount of calories. You have to reserve this list of food items containing calories.

You have to use the following three methods to reserve a list:

  • Inbuild method of python
  • List name [::-1] slicing trick
  • Swap the first element with the last one and second element with second last one and so on like, [6 7 8 34 5] -> [5 34 8 7 6]

Input-

Take a list as an input from the user

[5, 4, 1]

Output-

[1, 4, 5]

[1, 4, 5]

[1, 4, 5]

All three methods give the same results!

# Food and Calories
listinp = input("\nYour calorie list:\n: ")
listinp = listinp.split(",")
# Inbuilt Method
revlist1 = listinp.copy()
revlist1.reverse()
print(f"\nUsing Inbuilt method of Python {revlist1}\n")
# Using List slicing
print(f"Using List Slicing {listinp[::-1]}\n")
# Swaping elements
for i in range(len(listinp)//2):
listinp[i], listinp[len(listinp)-i-1] = listinp[len(listinp)-i-1], listinp[i]
print(f"Using List Slicing {listinp}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment