For this list
pays_list=["France","francais","€200", "1kg","20€","Espagne","espagnol","€20",
"Allemagne","allemand","deutsch","€100","2kg", "300€",
"Belgique","belge","frite","€30"]
pays_concatenate=[]
for i, elm in enumerate(pays_list):
if "€" in elm:
del pays_list[i]
pays_list=pays_list
for i in pays_list:
for e in i:
if any(e in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for e in i):
print(i)
"i" will be equal to elements with a capital letter...(France, Espagne etc ...)
I want to add the elements before the next capital letter
I except this output
pays_concatenate=["France francais","Espagne espagnol",
"Allemagne allemand deutsch",
"Belgique belge frite"]
Use itertools.groupby
to group the strings into those with and without a "€"
, and join each group with " "
:
import itertools
pays_list=["France","francais","€200", "1kg","20€","Espagne","espagnol","€20",
"Allemagne","allemand","deutsch","€100","2kg", "300€",
"Belgique","belge","frite","€30"]
pays_concatenate = [
" ".join(g)
for _, g in itertools.groupby(pays_list, lambda e: "€" in e)
]
print(pays_concatenate)
# ['France francais', '€200', '1kg', '20€', 'Espagne espagnol', '€20',
# 'Allemagne allemand deutsch', '€100', '2kg', '300€',
# 'Belgique belge frite', '€30']