Atakan Ak Atakan Ak - 19 days ago 5
Python Question

Can anybody show me an alternative to [::-1] ? I am trying to print a list in reverse order

infile=open("Integers.txt", "r")
ListIntegers=infile.readline().strip("\n").split(";")
def avgFirstThreeDigits(mylist):
list=[]
for i in range(0,len(mylist)):
sumFirstThreeDigits=(int(mylist[i][0])+int(mylist[i][1])+int(mylist[i][2]))
avg=sumFirstThreeDigits/3
list.append(avg)
print(list[::-1])
avgFirstThreeDigits(ListIntegers)

Answer

You can manually reverse a list with the following:

a = ['a', 'b', 'c', 'd']
b = [a[len(a) - i - 1] for i in range(len(a))]

>>> b
>>> ['d', 'c', 'b', 'a']
Comments