Home > Software design >  how can i loop like this but getting everything instead of only the 1st one
how can i loop like this but getting everything instead of only the 1st one

Time:02-19

i want loop like this but getting everything instead of only the 1st one.

from collections import OrderedDict

myList = OrderedDict(
                    {
                    'ID': ["1stID", "2ndID","3ndID"], 'ChannelID': ["1stChannel", "2ndChannel","3ndChannel"]
                    })
first_values = [v[0] for v in myList.values()]
print(first_values)

OUTPUT 
['1stID', '1stChannel']

instead of

['1stID', '1stChannel']
DESIRED OUTPUT:
['1stID', '1stChannel']
['2stID', '2stChannel']
['3stID', '3stChannel']

CodePudding user response:

You can use zip() to combine two list and get the combined pairs, as follows:

from collections import OrderedDict

myList = OrderedDict(
    {
        'ID': ["1stID", "2ndID", "3ndID"],
        'ChannelID': ["1stChannel", "2ndChannel", "3ndChannel"]
    })

pairs = zip(myList['ID'], myList['ChannelID'])
# list(pairs) -> [('1stID', '1stChannel'), ('2ndID', '2ndChannel'), ('3ndID', '3ndChannel')]

for pair in pairs:
    print(list(pair))

Result:

['1stID', '1stChannel']
['2ndID', '2ndChannel']
['3ndID', '3ndChannel']

CodePudding user response:

Other method (if you don't want to use key names):

from collections import OrderedDict

myList = OrderedDict(
    {
        "ID": ["1stID", "2ndID", "3ndID"],
        "ChannelID": ["1stChannel", "2ndChannel", "3ndChannel"],
    }
)

for a in zip(*myList.values()):
    print(a)  # or list(a) for lists instead of tuples

Prints:

('1stID', '1stChannel')
('2ndID', '2ndChannel')
('3ndID', '3ndChannel')

CodePudding user response:

You misunderstood the structure you are building so you are not accessing it correctly.

mylist is a 2 element dictionary. each of the element are lists of length 3. There is no connection between the positional elements of each other than the position.

See code below how I extracted and printed the data.
There may be an idiomatic way to do this in python more cleanly but this served to show the structure clearly.

from collections import OrderedDict
        
        myList = OrderedDict(
                            {
                            'ID':           ["1stID", "2ndID","3ndID"], 
                            'ChannelID':    ["1stChannel","2ndChannel","3ndChannel"]
                            })
        first_values = [v[0] for v in myList.values()]
        print(first_values)
    #
    # print out the values of the objects in the list  
      
        for v in myList.values():
            print(v)
    
    # extract the two lists        
        vid , vchan = myList.values()
    
    #print out the whole list in one statement
        print("all of vid ",vid)
        print("all of vchan ",vchan)
    
    #print out the element of the lists position by position    
        for i in range(len(vid)):
            print(i,"th list item " , vid[i],vchan[i])
  • Related