How to transform the list with dictionaries inside the line



  • Such a list

    ['Текст в списке вне словаря', {'Индекс 1': 'Элемент 1'}, {'Индекс 2': 'Элемент 1'}, {'Индекс 1': 'Элемент 2'}, {'Индекс 2': 'Элемент 2'}, {'Индекс 1': 'Элемент 3'}, {'Индекс 2': 'Элемент 3'}, 'Текст вне словаря']
    

    How do you turn this into a normal line?

    You should check if there's a dictionary on the list.

    I tried to convert the list into a line by various methods, but because of the vocabulary inside, I couldn't make it.



  • To see if there's such a code on the list of dictionaries:

    data = ['Текст в списке вне словаря', {'Индекс 1': 'Элемент 1'}, {'Индекс 2': 'Элемент 1'}, {'Индекс 1': 'Элемент 2'}, {'Индекс 2': 'Элемент 2'}, {'Индекс 1': 'Элемент 3'}, {'Индекс 2': 'Элемент 3'}, 'Текст вне словаря']
    

    dict_enabled = any(type(obj) == type(dict()) for obj in data)

    print(dict_enabled)

    Naturally, the code is not a classic, i.e., that if there is a dictionary on the list, it is an element of the list, not an element of the list, for example.

    I don't get it on the line. I need that code.

    res = []
    for obj in data:
    if type(obj) == type(str()):
    res.append(obj)
    elif type(obj) == type(dict()):
    for key in obj:
    res += [key, obj[key]]

    text = ' '.join(res)
    print(text)



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2