W
W
wolron2020-05-29 16:47:58
Python
wolron, 2020-05-29 16:47:58

How to truncate the characters in the list in each element: [' ']?

How to truncate the characters in the list in each element: [' ']

I need to get a list, but without extra elements .
Print should print only a list of words without parentheses or quotes.

There is a list:

[['Курица'] , ['Масло'], [' Гречка'], ['Молоко'], ['Мясо'], ['Рыба'], ['Хлеб'] ]

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
bbkmzzzz, 2020-05-29
@bbkmzzzz

And what for the list with lists from one element?
what separator, ksati?

x = [['Курица'] , ['Масло'], [' Гречка'], ['Молоко'], ['Мясо'], ['Рыба'], ['Хлеб']]
for i in x:
  print(i[0], end=' ')
print()

new_list = []
for i in x:
  new_list.append(i[0])
print(" ".join(new_list))

print(" ".join(i[0] for i in x))

ln = len(x)
stroke = ""
for i, j in enumerate(x):
  stroke += j[0]
  if i < ln:
    stroke += " "
print(stroke)

S
Sergey Karbivnichy, 2020-05-29
@hottabxp

Print should print only a list of words without parentheses or quotes.

It is possible like this:
my_list = [['Курица'] , ['Масло'], ['Гречка'], ['Молоко'], ['Мясо'], ['Рыба'], ['Хлеб'] ]

for l in my_list:
  print(l[0],end=' ')

>>> Курица Масло Гречка Молоко Мясо Рыба Хлеб
PS: I overlooked, the above has already given such an answer.

S
soremix, 2020-05-29
@SoreMix

a = [['Курица'] , ['Масло'], [' Гречка'], ['Молоко'], ['Мясо'], ['Рыба'], ['Хлеб'] ]
b = [el[0] for el in a]

Or
a = [['Курица'] , ['Масло'], [' Гречка'], ['Молоко'], ['Мясо'], ['Рыба'], ['Хлеб'] ]
print(','.join([el[0] for el in a]))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question