Nanda
Nanda

Reputation: 173

How to retrieve particular values from a list: python

I have a list of items

list = [{
            'id': '1',
            'elements': 'A',
            'table': 'path/to/table1',
            'chart': 'path/to/chart1',
        },
        {
            'id': '2',
            'elements': 'B',
            'table': 'path/to/table2',
            'chart': 'path/to/chart2',
        },
        {
            'id': '3',
            'elements': 'C',
            'table': 'path/to/table3',
            'chart': 'path/to/chart3',
        },]

selectionsFromTable = [{('A','2011','Table','Total'),
                       ('C','2011','Bar','Total'),
                       ('B','Pie','2001','Total')}]

Compare list['elements'] with selectionsFromTable items and if elem in selectionsFromTable == list['elements'] then append its respective table or chart to arr[]. Assume selectionsFromTable is form data getting from jquery. The index values and position of items always changes here. i am doing this.

arr = []
for data in list:
    if data['elements'] in selectionsFromTable: # Suggest condition here
        inner = []
        if 'Table' in selectionsFromTable:
            print("table")
            inner.append({'id': data['id'],
                          'section_title': data['elements'],
                          'tableorChart': data['table'],
                          })

        elif 'Bar' in selectionsFromTable or 'Pie' in selectionsFromTable :
            print("chart")
            inner.append({'id': data['id'],
                          'section_title': data['elements'],
                          'tableorChart': data['chart'],
                          })

        arr.append(inner)

I believe it is wrong, kindly suggest me logic here. I am not able to go to "elif" condition as my selectionsFromTable contains "Table".

Upvotes: 4

Views: 402

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90869

According to the example you have given, the following check would not work-

data['elements'] in selectionsFromTable

This is because, selectionsFromTable , contains set types, whereas data['elements'] is a string . You want to check inside each element (set) in selectionsFromTable .

A simple way to do this -

arr = []
for data in list:
    elem = next((s for s in selectionsFromTable if data['elements'] in s), None)
    if elem:
        inner = []
        if 'Table' in elem:
            print("table")
            inner.append({'id': data['id'],
                          'section_title': data['elements'],
                          'tableorChart': data['table'],
                          })

        elif ('Bar' in elem) or ('Pie' in elem):
            print("chart")
            inner.append({'id': data['id'],
                          'section_title': data['elements'],
                          'tableorChart': data['chart'],
                          })

        arr.append(inner)

Main change would be the line -

elem = next((s for s in selectionsFromTable if data['elements'] in s), None)

What this does is that it either tries to take the first element in selectionsFromTable in which data['elements'] , or if no such elements exists (that is the generator expression did not yield even a single value) , it returns None .

Then in next line we check if elem is not None and then do similar logic after that based on elem (not selectionsFromTable ).

Also, you should not used list as the name of a variable, it would end up masking the list() built-in function and you would not be able to use it afterwards in the same script.


Example/Demo -

>>> list = [{
...             'id': '1',
...             'elements': 'A',
...             'table': 'path/to/table1',
...             'chart': 'path/to/chart1',
...         },
...         {
...             'id': '2',
...             'elements': 'B',
...             'table': 'path/to/table2',
...             'chart': 'path/to/chart2',
...         },
...         {
...             'id': '3',
...             'elements': 'C',
...             'table': 'path/to/table3',
...             'chart': 'path/to/chart3',
...         },]
>>>
>>> selectionsFromTable = [{'A','2011','Table','Total'},
...                        {'C','2011','Bar','Total'},
...                        {'B','Pie','2001','Total'}]
>>> arr = []
>>> for data in list:
...     elem = next((s for s in selectionsFromTable if data['elements'] in s), None)
...     if elem:
...         inner = []
...         if 'Table' in elem:
...             print("table")
...             inner.append({'id': data['id'],
...                           'section_title': data['elements'],
...                           'tableorChart': data['table'],
...                           })
...         elif ('Bar' in elem) or ('Pie' in elem):
...             print("chart")
...             inner.append({'id': data['id'],
...                           'section_title': data['elements'],
...                           'tableorChart': data['chart'],
...                           })
...         arr.append(inner)
...
table
chart
chart
>>> import pprint
>>> pprint.pprint(arr)
[[{'id': '1', 'section_title': 'A', 'tableorChart': 'path/to/table1'}],
 [{'id': '2', 'section_title': 'B', 'tableorChart': 'path/to/chart2'}],
 [{'id': '3', 'section_title': 'C', 'tableorChart': 'path/to/chart3'}]]

Upvotes: 3

Related Questions