From a list of words of different lenghts I need to make a list of letters of the same index of all words
Question:
Need to create lists with the letters of the words of the first list by index. For example the first output list for the first index of all the words will be [‘l’, ‘c’, ‘e’, ‘t’, ‘s’, ‘a’, ‘s’, ‘m’, ‘p’]. I have solved this problem in a certain way, but im out of time in some tests that i have to do on this code.
This is my code:
l = ['lion', 'cat', 'elephant', 'tiger', 'spyder', 'alligator','snake', 'monkey','penguin']
new_list = []
x = 0
y = 0
for words in l:
z = len(words)
if x <= z:
x = z
while y != x:
ultimate_list = []
for words in l:
try:
ultimate_list.append(words[y])
except Exception:
pass
new_list.append(ultimate_list)
y += 1
print(new_list)
Thanks for the help!
Answers:
Here you want to use list comprehension:
l = ['lion', 'cat', 'elephant', 'tiger', 'spyder', 'alligator', 'snake', 'monkey', 'penguin']
max_len = max(map(len, l))
ultimate_list = []
for i in range(max_len):
index_letters = [e[i] for e in l if len(e) > i]
ultimate_list.append(index_letters)
print(ultimate_list)
What is happening here:
1 max_len
– map
is applying the function len
to all elements of the list, and thus calculates the longest word
2 we are going by the len of the word and by using list comprehension adding appropriate index letters, if length of the word is bigger than the current index
l = ['lion', 'cat', 'elephant', 'tiger', 'spyder', 'alligator', 'snake', 'monkey', 'penguin']
longest_string=max(l,key=len)
[[e[i] for e in l if len(e) > i] for i in range(len(longest_string))]
#output
[['l', 'c', 'e', 't', 's', 'a', 's', 'm', 'p'],
['i', 'a', 'l', 'i', 'p', 'l', 'n', 'o', 'e'],
['o', 't', 'e', 'g', 'y', 'l', 'a', 'n', 'n'],
['n', 'p', 'e', 'd', 'i', 'k', 'k', 'g'],
['h', 'r', 'e', 'g', 'e', 'e', 'u'],
['a', 'r', 'a', 'y', 'i'],
['n', 't', 'n'],
['t', 'o'],
['r']]
Need to create lists with the letters of the words of the first list by index. For example the first output list for the first index of all the words will be [‘l’, ‘c’, ‘e’, ‘t’, ‘s’, ‘a’, ‘s’, ‘m’, ‘p’]. I have solved this problem in a certain way, but im out of time in some tests that i have to do on this code.
This is my code:
l = ['lion', 'cat', 'elephant', 'tiger', 'spyder', 'alligator','snake', 'monkey','penguin']
new_list = []
x = 0
y = 0
for words in l:
z = len(words)
if x <= z:
x = z
while y != x:
ultimate_list = []
for words in l:
try:
ultimate_list.append(words[y])
except Exception:
pass
new_list.append(ultimate_list)
y += 1
print(new_list)
Thanks for the help!
Here you want to use list comprehension:
l = ['lion', 'cat', 'elephant', 'tiger', 'spyder', 'alligator', 'snake', 'monkey', 'penguin']
max_len = max(map(len, l))
ultimate_list = []
for i in range(max_len):
index_letters = [e[i] for e in l if len(e) > i]
ultimate_list.append(index_letters)
print(ultimate_list)
What is happening here:
1 max_len
– map
is applying the function len
to all elements of the list, and thus calculates the longest word
2 we are going by the len of the word and by using list comprehension adding appropriate index letters, if length of the word is bigger than the current index
l = ['lion', 'cat', 'elephant', 'tiger', 'spyder', 'alligator', 'snake', 'monkey', 'penguin']
longest_string=max(l,key=len)
[[e[i] for e in l if len(e) > i] for i in range(len(longest_string))]
#output
[['l', 'c', 'e', 't', 's', 'a', 's', 'm', 'p'],
['i', 'a', 'l', 'i', 'p', 'l', 'n', 'o', 'e'],
['o', 't', 'e', 'g', 'y', 'l', 'a', 'n', 'n'],
['n', 'p', 'e', 'd', 'i', 'k', 'k', 'g'],
['h', 'r', 'e', 'g', 'e', 'e', 'u'],
['a', 'r', 'a', 'y', 'i'],
['n', 't', 'n'],
['t', 'o'],
['r']]