0

the results of the following code is: ['di', 'me', 'medesimo', 'meco', 'mi', 'vergogno'] di me me me mi ve

How can I change the second list: di me me me mi ve

into a list of this type: ['di', 'me', 'me', 'me', 'mi', 've']

Thank you in advance!

txt="Di me medesimo meco mi vergogno"
a=txt.lower().split()
print(a)
for x in a:
    print (x[0:2])

I expect to be able to have a list from which I can code a way to know if there are two or more elements that are the same. I am looking for alliteration.

Andrea C
  • 3
  • 2

2 Answers2

2

You can do it like this:

>>> txt="Di me medesimo meco mi vergogno"
>>> l = [x[:2] for x in txt.lower().split()]
>>> l
['di', 'me', 'me', 'me', 'mi', 've']
>>> 
Ayoub
  • 1,417
  • 14
  • 24
1

Do this:

txt="Di me medesimo meco mi vergogno"
a=txt.lower().split()
print(a)
list2 = []
for x in a:
    list2.append(x[0:2])

print(list2)

Output

['di', 'me', 'medesimo', 'meco', 'mi', 'vergogno']
['di', 'me', 'me', 'me', 'mi', 've']
arajshree
  • 626
  • 4
  • 13