Related
normal = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
modified = ['s', 'n', 'v', 'f', 'r', 'g', 'h', 'j', 'o', 'k', 'l', 'a',
'z', 'm', 'p', 'q', 'w', 't', 'd', 'y', 'i', 'b', 'e', 'c', 'u', 'x']
word = input()
for char in word:
if char in normal:
char.replace(char, modified)
This is what i have so far,
I want to be able to type in a sentence and it will output the sentence with the modified alphabets
one of the way to map the normal list character with the modified list character and replace them in the sentence.
normal = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
modified = ['s', 'n', 'v', 'f', 'r', 'g', 'h', 'j', 'o', 'k', 'l', 'a',
'z', 'm', 'p', 'q', 'w', 't', 'd', 'y', 'i', 'b', 'e', 'c', 'u', 'x']
mapper = {}
for a, b in zip(normal, modified):
mapper[b] = a
word ="this is user commented word"
# new_word = ' '.join(map(lambda x: ''.join(mapper[i] for i in x), word.split()))
new_word = []
for i in word.split():
tmp = []
for j in i:
tmp.append(mapper[j])
new_word.append(''.join(tmp))
new_sentence = ' '.join(new_word)
print(new_sentence)
output
rgua ua yawe xinnwbrws qies
normal = 'abcdefghijklmnopqrstuvwxyz'
modified = 'lvxswdfguhjknbiopearycqztm'
word = input()
convert = str.maketrans(normal, modified)
print(word.translate(convert))
This question already has answers here:
Iterate an iterator by chunks (of n) in Python?
(14 answers)
Closed 2 years ago.
Here's a list that I have,
data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
Here data is a generator and I want to iterate over it and prepared batches of 12 equal datapoints, if it is less than 12 in last batch I need it too, but below code is not working,
subsets = []
subset = []
for en, i in enumerate(data):
if en % 12 == 0 and en > 0:
subsets.append(subset)
subset = []
else:
subset.append(i)
print(subsets)
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
['z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']]
But my code is not working properly because the first nested list has 12 values but rest of it have 11 values and it missed out last few values which are less than 12 in the last batch
Expected Output:
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
['y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'],
['v', 'w', 'x', 'y', 'z']]
Two changes, you need to start iterating from 1 and append in the sublist before emptying it:
data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
subsets = []
subset = []
# start counting from index '1'
for en, i in enumerate(data, 1):
if en % 12 == 0 and en > 0:
# append the current element before emptying 'subset'
subset.append(i)
subsets.append(subset)
subset = []
else:
subset.append(i)
# append the left-over sublist/subset to your main list as well
subsets.append(subset)
for i in subsets:
print(i)
gives
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i']
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']
['v', 'w', 'x', 'y', 'z']
Alternative solution is using buit-in itertools.islice. You can check to see which approach is faster or more convenient. Kr.
import itertools
def gen_sublist(your_iter, size):
while True:
part = tuple(itertools.islice(your_iter, size))
if not part:
break
yield part
data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
for c in gen_sublist(data, size=12):
print(c)
which returns:
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l')
('m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x')
('y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i')
('j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u')
('v', 'w', 'x', 'y', 'z')
A different approach which does not use modulo or enumeration (just another option since other answers already correct your approach):
In [1]: subsets = []
In [2]: data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
In [3]:
...: while True:
...: try:
...: x = []
...: for i in range(12):
...: x.append(next(data))
...: subsets.append(x)
...: except: # Catch StopIteration Exception when generator runs out of values
...: subsets.append(x)
...: break
...:
Outputs:
In [4]: subsets
Out[4]:
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
['y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'],
['v', 'w', 'x', 'y', 'z']]
For ex:
a = "pandaxngeqrymtso-ezmlaesowxaqbujl-noilktxreecytrql-gskaboofsfoxdtei-utsmakotufodhlrd-iroachimpanzeesa-nintrwflyrkhcdum-jcecahkktiklsvhr-mhvsbaykagodwgca-koalatcwlkfmrwbb-jsrrfdolphinuyt"
a = a.split("-")
mylist = []
word = ""
while i < len(a[0]): #16
for elem in a:
word+= elem[i]
mylist.append(kelime)
i += 1
word = ""
I just want a list which contains "penguinjmhkj, azostrichos..." But I get an Index error.
What can I do?
You can try this:
>>> word = [''.join(letters) for letters in zip(*(list(word) for word in a.split('-')))]
>>> word
['penguinjmkj',
'azostrichos',
'nmiksonevar',
'dllamatcslr',
'aakbacrabaf',
'xetokhwhatd',
'nsxooifkyco',
'gorftmlkkwl',
'ewesupytalp',
'qxeffarigkh',
'racoonkkofi',
'yqyxdzhldmn',
'mbtdhecswru',
'turtledvgwy',
'sjqersuhcbt']
Explanation:
You can understand what it is doing if you print the individual parts,
for e.g.:
>>> print(*(list(word) for word in a.split('-'))
['p', 'a', 'n', 'd', 'a', 'x', 'n', 'g', 'e', 'q', 'r', 'y', 'm', 't', 's', 'o'] ['e', 'z', 'm', 'l', 'a', 'e', 's', 'o', 'w', 'x', 'a', 'q', 'b', 'u', 'j', 'l'] ['n', 'o', 'i', 'l', 'k', 't', 'x', 'r', 'e', 'e', 'c', 'y', 't', 'r', 'q', 'l'] ['g', 's', 'k', 'a', 'b', 'o', 'o', 'f', 's', 'f', 'o', 'x', 'd', 't', 'e', 'i'] ['u', 't', 's', 'm', 'a', 'k', 'o', 't', 'u', 'f', 'o', 'd', 'h', 'l', 'r', 'd'] ['i', 'r', 'o', 'a', 'c', 'h', 'i', 'm', 'p', 'a', 'n', 'z', 'e', 'e', 's', 'a'] ['n', 'i', 'n', 't', 'r', 'w', 'f', 'l', 'y', 'r', 'k', 'h', 'c', 'd', 'u', 'm'] ['j', 'c', 'e', 'c', 'a', 'h', 'k', 'k', 't', 'i', 'k', 'l', 's', 'v', 'h', 'r'] ['m', 'h', 'v', 's', 'b', 'a', 'y', 'k', 'a', 'g', 'o', 'd', 'w', 'g', 'c', 'a'] ['k', 'o', 'a', 'l', 'a', 't', 'c', 'w', 'l', 'k', 'f', 'm', 'r', 'w', 'b', 'b'] ['j', 's', 'r', 'r', 'f', 'd', 'o', 'l', 'p', 'h', 'i', 'n', 'u', 'y', 't']
So it breaks up all the individual words delimited by - into characters.
Then zip does this:
>>> print(zip(*(list(word) for word in a.split('-'))))
('p', 'e', 'n', 'g', 'u', 'i', 'n', 'j', 'm', 'k', 'j') ('a', 'z', 'o', 's', 't', 'r', 'i', 'c', 'h', 'o', 's') ('n', 'm', 'i', 'k', 's', 'o', 'n', 'e', 'v', 'a', 'r') ('d', 'l', 'l', 'a', 'm', 'a', 't', 'c', 's', 'l', 'r') ('a', 'a', 'k', 'b', 'a', 'c', 'r', 'a', 'b', 'a', 'f') ('x', 'e', 't', 'o', 'k', 'h', 'w', 'h', 'a', 't', 'd') ('n', 's', 'x', 'o', 'o', 'i', 'f', 'k', 'y', 'c', 'o') ('g', 'o', 'r', 'f', 't', 'm', 'l', 'k', 'k', 'w', 'l') ('e', 'w', 'e', 's', 'u', 'p', 'y', 't', 'a', 'l', 'p') ('q', 'x', 'e', 'f', 'f', 'a', 'r', 'i', 'g', 'k', 'h') ('r', 'a', 'c', 'o', 'o', 'n', 'k', 'k', 'o', 'f', 'i') ('y', 'q', 'y', 'x', 'd', 'z', 'h', 'l', 'd', 'm', 'n') ('m', 'b', 't', 'd', 'h', 'e', 'c', 's', 'w', 'r', 'u') ('t', 'u', 'r', 't', 'l', 'e', 'd', 'v', 'g', 'w', 'y') ('s', 'j', 'q', 'e', 'r', 's', 'u', 'h', 'c', 'b', 't')
So it takes all the corresponding characters from each group, each of the tuples get passed as letters in each iteration in the main code.
Then you join each tuple, for e.g. in first iteration:
>>> ''.join(('p', 'e', 'n', 'g', 'u', 'i', 'n', 'j', 'm', 'k', 'j'))
'penguinjmkj'
[<item after some operation> for <each item> in <item_list>] this structure is called list comprehension. * is used for iterable unpacking.
it took a while but i cracked it;
what you just need to do is to handle the Index Error: String out of range.
if you count the words they are over 16 while the array items after splitting are just over 11. to cut long story short; Handle the exception with a try_except where you are appending the letters at:
word+= elem[i]
here is my code and how i solved it using try_catch
a = "pandaxngeqrymtso-ezmlaesowxaqbujl-noilktxreecytrql-gskaboofsfoxdtei-utsmakotufodhlrd-iroachimpanzeesa-nintrwflyrkhcdum-jcecahkktiklsvhr-mhvsbaykagodwgca-koalatcwlkfmrwbb-jsrrfdolphinuyt"
newArr = a.split('-')
newWord = []
i = 0
mylist = []
while i < len(newArr[0]):
word = ""
for item in newArr:
try:
word += item[i]
except:
break
i += 1
mylist.append(word)
print(mylist)
I used a try_except to handle the Index Error when appending the letter, then break when ever the 'i' used for the while loop is greater than the newArr length in the for loop.
try for your self!
Similar to the code from Sayandip, but it would have been more readable for me in the past:
mylist =[]
for element in zip(*a.split('-')):
mylist.append(''.join(element))
print(mylist)
I get
['penguinjmkj', 'azostrichos', 'nmiksonevar', 'dllamatcslr', 'aakbacrabaf', 'xetokhwhatd', 'nsxooifkyco', 'gorftmlkkwl', 'ewesupytalp', 'qxeffarigkh', 'racoonkkofi', 'yqyxdzhldmn', 'mbtdhecswru', 'turtledvgwy', 'sjqersuhcbt']
I am moving the splitting in the for loop, and using the * construct to pass the whole list resulting from splitting, see usage here and here.
So I'm trying to add a new list to a 2d array inside a for loop, by taking the last letter of the list and putting it to the start, and adding this new list to the 2d array.
But although the new lists work, when I append to the 2d array, it just appends the normal alphabet, not my new one.
Can anybody see what I'm doing wrong?
def tabulaRecta():
import string
alpha = list(string.ascii_uppercase)
l2d = []
l2d.append(alpha)
for i in range(26):
temp = alpha[len(alpha)-1]
alpha.pop()
alpha.insert(0,temp)
l2d.append(alpha)
print(l2d)
tabulaRecta()
The program is supposed to output something like this, but all of these within a list:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']
['Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X']
['X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W']
['W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V']
['V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U']
['U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T']
['T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S']
['S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']
['R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q']
['Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']
['P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
['O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N']
['N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
['M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
['L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
['K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
['J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
['I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
['H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
['G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F']
['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E']
['E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D']
['D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C']
['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B']
['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
instead it prints this 26 times in a list:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Instead of saying print(...) you can append it to a list and print it when the loop is finished
s = list(string.ascii_uppercase)
>>> for i in range(len(s),-1,-1):
... print(s[i:]+s[:i])
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']
['Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X']
['X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W']
.
.
.
.
A 2d array is an array of pointers to arrays, and in your code each one points to the same list, that you are modifying 26 times eventually coming back to the original one.
You have to make a copy of the list each time you modify it.
For example, add a alpha = list(alpha) at the beginning of the for loop:
def tabulaRecta():
import string
alpha = list(string.ascii_uppercase)
l2d = []
l2d.append(alpha)
for i in range(26):
alpha = list(alpha)
temp = alpha[len(alpha) - 1]
alpha.pop()
alpha.insert(0, temp)
l2d.append(alpha)
print(l2d)
tabulaRecta()
You could also just do this:
import string
def tabula_rect():
alpha = list(string.ascii_uppercase)
l2d = []
l2d.append(alpha)
for i in range(len(alpha)):
temp = alpha[len(alpha)-1]
alpha = alpha[:-1]
alpha.insert(0, temp)
l2d.append(alpha)
return l2d
I need help rearranging this list alphabetically
list= ['z', 'a', 'b', 'y', 'c', 'x', 'd', 'w', 'e', 'v', 'f', 'g', 'u', 'h', 'i', 'j', 't' ,'k', 'l', 's', 'm', 'n', 'r', 'o', 'p', 'q', ' ']
into "hello world" by indexing into the array. How exactly do I do that? I'm a beginner and I'm doing this in python 2.7.
As has been mentioned, your list can be sorted alphabetically by using the sort() function as follows:
mylist = ['z', 'a', 'b', 'y', 'c', 'x', 'd', 'w', 'e', 'v', 'f', 'g', 'u', 'h', 'i', 'j', 't' ,'k', 'l', 's', 'm', 'n', 'r', 'o', 'p', 'q', ' ']
mylist.sort()
print mylist
Which results in your list looking like:
[' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
But you go on to say 'index into' for 'hello world'. If you mean you want to create a simple cypher then this could be easily be achieved as follows:
import string
s_from = 'abcdefghijklmnopqrstuvwxyz '
s_to = 'zabycxdwevfguhijtklsmnropq '
cypher_table = string.maketrans(s_from, s_to)
print "hello world".translate(cypher_table)
This would convert your text as follows:
wcggi rikgy
Please could you edit your question to give an example of what you are trying to achieve.
Since there is a empty element in the list. You can try to avoid them by using sorted method of list
>>> sorted(list_alphabet, key=list_alphabet.remove(' '))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
sorted does create new copy of actual list and sort them. So you can refer the output of the sorted to new variable. Like
>>> sorted_list_alphabet = sorted(list_alphabet, key=list_alphabet.remove(' '))
>>> sorted_list_alphabet
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
PS: do not use list as a variable name because it brings conflict between actual list and your list variable