-4

I had a list that has Business = ['Company name','Mycompany',Revenue','1000','Income','2000','employee','3000','Facilities','4000','Stock','5000'] , the output of the list structure is shown below:

Company        Mycompany
Revenue        1000
Income         2000
employee       3000
Facilities     4000
Stock          5000

The Dynamic list gets updated ***

for every iteration for the list and some of the items in the list is missing

***. for example execution 1 the list gets updated as below:

Company        Mycompany
Income         2000             #revenue is missing
employee       3000          
Facilities     4000
Stock          5000

In the above list the Revenue is removed from list as company has no revenue, in second example :

Company        Mycompany
Revenue        1000
Income         2000                
Facilities     4000              #Employee is missing
Stock          5000

In the above example 2 Employee is missing. How to create a output list that replaces the missing values with 0, in example 1 revenue is missing , hence I have to replace the output list with ['Revenue,'0'] at its position, for better understanding please find below

output list created for example 1: Revenue replaced with 0

Company Mycompany| **Revenue 0**| Income 2000| employee 3000| Facilities 4000| Stock 5000

Output list for example 2: employee is replaced with 0

Company Mycompany| Revenue 1000| Income 2000| **employee 0**| Facilities 4000| Stock 5000

How can I achieve the output list with replacing the output list with 0 on missing list items without changing the structure of list. My code so far:

       for line in Business:
        if 'Company' not in line:
            Business.insert( 0, 'company')
            Business.insert( 1, '0')
        if 'Revenue' not in line:
            #got stuck here
        if 'Income' not in line:
            #got stuck here
        if 'Employee' not in line:
            #got stuck here
        if 'Facilities' not in line:
            #got stuck here
        if 'Stock' not in line:
            #got stuck here

Thanks a lot in advance

2
  • 1
    A dictionary seems to be a much better data structure for your task. Commented Jun 13, 2021 at 6:09
  • 1
    This wold be much easier if you could change to dicts. Commented Jun 13, 2021 at 6:10

3 Answers 3

2

If you are getting inputs as a list then you can convert the list into a dict like this then you'll have a better approach on data, getting as a dictionary would be a better choice though

Business = ['Company name','Mycompany','Revenue',1000,'Income',2000,'employee',3000,'Facilities',4000,'Stock',5000]

BusinessDict = {Business[i]:Business[i+1] for i in range(0,len(Business)-1,2)}
print(BusinessDict)
Sign up to request clarification or add additional context in comments.

Comments

1

As said in the comments, a dict is a much better data structure for the problem. If you really need the list, you could use a temporary dict like this:

example = ['Company name','Mycompany','Income','2000','employee','3000','Facilities','4000','Stock','5000']
template = ['Company name', 'Revenue', 'Income', 'employee', 'Facilities', 'Stock']

# build a temporary dict
exDict = dict(zip(example[::2], example[1::2]))

# work on it
result = []
for i in template:
    result.append(i)
    if i in exDict.keys():
        result.append(exDict[i])
    else:
        result.append(0)

A bit more efficient (but harder to understand for beginners) would be to create the temporary dict like this:

i = iter(example)
example_dict = dict(zip(i, i))

This works because zip uses lazy evaluation.

Comments

1

You can use dictionary like this:

d={'Company':0,'Revenue':0,'Income':0,'employee':0,'Facilities':0,'Stock':0}
given=[['Company','Mycompany'],['Income',2000],['employee',3000],['Facilities',4000],['Stock',5000]]
for i in given:
    d[i[0]]=i[1]
ans=[]
for key,value in d.items():
    ans.append([key,value])

1 Comment

The given data structure, is not the one that OP has. ([[key1, value1], [key2, value2],...]) instead of ([key1, value1, key2, value2,...])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.