Skip to content

Commit 2ddae8d

Browse files
committed
add sorted() method example
1 parent cf6af1f commit 2ddae8d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

03-loops/l5.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# Use the `sorted()` method to sort a list (here, alphabetically) and store
3+
# the results in a new variable. The original list variable is untouched.
4+
5+
shopping_list = ["Tomatoes", "Bananas", "Crackers",
6+
"Sugar", "Icecream", "Bread", "Bananas", "Chocolate"]
7+
8+
9+
print("Printout of shopping_list:")
10+
for item in shopping_list:
11+
print(item)
12+
13+
14+
# Sort the list using `sorted()` and store the results in a new variable
15+
sorted_shopping_list = sorted(shopping_list)
16+
17+
print()
18+
print("Printout of sorted_shopping_list:")
19+
for item in sorted_shopping_list:
20+
print(item)

0 commit comments

Comments
 (0)