Reputation: 67
I have the following piece of code:
for response_i in response['response']:
var_1=None
if response_i['objkey'] == 'explorecube_data':
var_1=response_i['objcontent'][0]['title']
if response_i['objkey'] == 'explorecube_data' and response_i['objcontent'][0]['title'] == var_1:
print(var_1)
dim_1=None
if response_i['objkey'] == 'explorecube_dimvalues':
dim_1=response_i['objcontent'][0]['title']
if response_i['objkey'] == 'explorecube_dimvalues' and response_i['objcontent'][0]['title'] == dim_1:
print(dim_1)
ab=[var_1, dim_1]
print(ab)
I updated the question. following is the output:
['tos', None]
[None, None]
[None, 'lat']
[None, None]
I would like to have the following output:
['tos', 'lat']
I appreciate any suggestion to solve this issue
Upvotes: 1
Views: 908
Reputation: 1958
You can simply append values to ab
, when the values are not None
.Then you will need to instantiate and print ab
outside of the loop.
ab = []
for response_i in response['response']:
var_1=None
if response_i['objkey'] == 'explorecube_data':
var_1=response_i['objcontent'][0]['title']
if response_i['objkey'] == 'explorecube_data' and response_i['objcontent'][0]['title'] == var_1:
print(var_1)
ab.append(var1)
dim_1=None
if response_i['objkey'] == 'explorecube_dimvalues':
dim_1=response_i['objcontent'][0]['title']
if response_i['objkey'] == 'explorecube_dimvalues' and response_i['objcontent'][0]['title'] == dim_1:
print(dim_1)
ab.append(dim1)
print(ab)
Upvotes: 0
Reputation: 895
Use the generator to produce the result that you want, then print it.
def PrintGen():
for response_i in response['response']:
if response_i['objkey'] == 'explorecube_data' or response_i['objkey'] == 'explorecube_dimvalues':
yield response_i['objcontent'][0]['title']
ab =PrintGen()
listab =list(ab)
print(listab)
Generators are used to yield objects which meet the demand, they are proper for you.
Upvotes: 2
Reputation: 845
try something like this, should convert the data into a pandas dataframe and drop the None values
import pandas as pd
data = []
data.append([var_1, dim_1])
df = pd.DataFrame(data, columns=['var_1', 'dim_1'])
df = df.dropna(how = 'all')
Upvotes: 0