pythontextorientationskewtext-rotation

Python create list based on letter index of existing list


So I am trying to work on a script which allows me to take a text file, rotate the layout 90 degrees and keep all the letters in the correct orientation (as opposed to having the letters turn sideways)

Right so If I have a list like so:

x = ['the','boy','ate','jam','pie']

How can I create a list based off of the index position of the letters within each list item.

letters[0] = t,b,a,j,p
letters[1] = h,o,t,a,i
letters[2] = e,y,e,m,e

x = ['tbajp','hotai','eyeme']

Where I am at:

new_list = []

x = ["the","boy","ate","jam","pie"]

y = len(x)

t = 0
while y > 0:

  z = len(x[0])
  c = 0

  while z > 0:
    print(x[t][c])
    z -= 1
    c += 1

  y -= 1
  t += 1

Solution

  • Shortest method using list(zip(*x)):

    x = ['the','boy','ate','jam','pie']
    rotate_list = [''.join(i) for i in list(zip(*x))]
    print(rotate_list)
    

    Output:

    ['tbajp', 'hotai', 'eyeme']
    

    Edit:

    You can reverse the rotate_list by using:

    reversed_strings = [''.join(i)[::-1] for i in list(zip(*x))]
    

    Output:

    ['pjabt', 'iatoh', 'emeye']