Answer to Question #187598 in Python for Kofi

Question #187598

1.Use a for loop and the list append method to generate the powers of 2 to produce the following result [1, 2, 4, 8, 16, 32, 64, 128]

2.Write a for loop that prints a dictionary's items in sorted (ascending) order


1
Expert's answer
2021-05-03T02:10:30-0400

Question 1:

lst = []
for i in range(8):
    x = (2**i)
    lst.append(x)
print(lst)


Question 2:

import operator
# dictionary  
d = {1: 3, 3: 2, 4: 1, 2: 3, 0: 0}
# sorting
sorted_d = sorted(d.items(), key=operator.itemgetter(1))
# printing sorted dict.
print('Dictionary in ascending order by value : ',sorted_d)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS