Home > Mobile >  How to create a list with several lists python
How to create a list with several lists python

Time:05-19

I have these 3 lists :

list1 (question):

['Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)', 'The Scrum Team consists of: (choose all that apply)']

list2 (correct) : (list of list)

[['It is likely to increase customer engagement and happiness with the product.', 'It reduces long-term operational costs.'], ['The Developers', 'The Scrum Master', 'The Product Owner']]

list3 (incorrect) : (list of list)

[['It has all User Stories that were committed to at the Sprint Planning.', 'It is completed on time.', 'It is approved by the Product Owner at the Sprint Review.'], ['The Key Stakeholders']]

I want to create a list that will contains these 3 lists it should look like that :

[{'question': 'Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)',
  'correct': ['It is likely to increase customer engagement and happiness with the product',
   'It reduces long-term operational costs.'],
  'incorrect': ['It has all User Stories that were committed to at the Sprint Planning.',
   'It is completed on time.]},

{'question': 'The Scrum Team consists of: (choose all that apply)',
  'correct': ['The Developers',
   'The Scrum Master',
   'The Product Owner'],
  'incorrect': ['The Key Stakeholders']} ]

so for each question, we get the correct answer(s) and incorrect answer(s) and need to add "question :" / "correct:" / "incorrect" as well. if anyone could help me please

CodePudding user response:

I would say the best solution is:

lol = [{'question': x[0], 'correct': x[1], 'incorrect': x[2]} for x in zip(list1, list2, list3)]

The idea is to zip() the list, it's a function that takes a couple (1 ) of iterables/containers and joins them into one list. For example:

In [1]: list(zip([1, 2], [3, 4]))                                                                                                                                       
Out[1]: [(1, 3), (2, 4)]

And from here, we just iterate over the zipped list, and from each tuple, we create a dict.

CodePudding user response:

Try this approach with multiple uses of zip. This would work in the following way (refer diagram for visual intuition) -

enter image description here

  1. First, zip each corresponding element in each of the 3 lists. This will result in a list with 2 items, where each item is a tuple with 3 elements (Q, C, I)
  2. Second, zip the keys (containing key names/variable names) with each element of the above zip object. This will result in (Q0, C0, I0) zipped with ["question", "correct", "incorrect"] to look like [("question",Q0), ("correct":C0), ("incorrect":I0)] and same for (Q1, C1, I1).
  3. Finally dict() will convert this into key, value pairs like you are looking for.
question = ['Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)', 'The Scrum Team consists of: (choose all that apply)']
correct = [['It is likely to increase customer engagement and happiness with the product.', 'It reduces long-term operational costs.'], ['The Developers', 'The Scrum Master', 'The Product Owner']]
incorrect = [['It has all User Stories that were committed to at the Sprint Planning.', 'It is completed on time.', 'It is approved by the Product Owner at the Sprint Review.'], ['The Key Stakeholders']]
keys = ["question", "correct", "incorrect"]

output = [dict(zip(keys,i)) for i in zip(question, correct, incorrect)] #<-----

print("First dictionary")
print(output[0])
print("Second dictionary")
print(output[1])
First dictionary
{'question': 'Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)', 
 'correct': ['It is likely to increase customer engagement and happiness with the product.', 'It reduces long-term operational costs.'], 
 'incorrect': ['It has all User Stories that were committed to at the Sprint Planning.', 'It is completed on time.', 'It is approved by the Product Owner at the Sprint Review.']}


Second dictionary
{'question': 'The Scrum Team consists of: (choose all that apply)', 
 'correct': ['The Developers', 'The Scrum Master', 'The Product Owner'], 
 'incorrect': ['The Key Stakeholders']}

CodePudding user response:

You can nest Python lists by just nesting them into another.

E.g.

nestedList = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']

The curly braces you use above usally define dictionaries.



If you still want to go for lists - even though a dict would fit quite nicely - you could go for something like this:

[['question1 delimiter answer'], ['question2 delimiter answer'], ... ] 

or

[['question1']['delimiter']['answer'], ....] 

or

[[['question1']['delimiter']['answer']], ....] 
  • Related