Advertisement
Guest User

Untitled

a guest
Feb 1st, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. def even_spacing(lst):
  2.     new_lst = []  # list for the final output
  3.     index_colon = []  # list of the indexs where ':' is found, for each line
  4.  
  5.     # find where the ':' in each line
  6.     for line_string in lst:
  7.         index_colon.append(line_string.find(':'))
  8.     highest_index = max(index_colon)  # the highest index ':' can be found at in any of the lines
  9.  
  10.     # we add the extra spaces
  11.     for index, s in enumerate(lst):
  12.         difference = highest_index - index_colon[index]  # how many spaces will be needed
  13.  
  14.  
  15.         # METHOD 1 not working, less code
  16.         # new_lst.append(''.join(list(s).insert(index_colon[index]+1, ' '*difference)))  # insert at the index we found the proper number of spaces
  17.  
  18.         # METHOD 2, working, but more code
  19.         list_letters = list(s)
  20.         list_letters.insert(index_colon[index]+1, ' '*difference)
  21.         new_lst.append(''.join(list_letters))
  22.  
  23.     return new_lst
  24.  
  25. test = ['last name: Callum', 'first name: Brian', 'middle name: Mosbey', 'balance: $0']
  26. print even_spacing(test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement