Printing only one index of a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanazzle
    New Member
    • Oct 2007
    • 10

    Printing only one index of a list

    If i had a list of lists, for example:

    list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

    how can i write a function that only writes out index[1] of each list in the list?

    i need the output to contain 3,5,4,2,2

    and then my final goal is to create a loop that goes through the major list and only print out index[0] of the minor lists if they are not in index[1], so in this case, 4 and 9. I tried using a loop in a loop, but its not working. any ideas?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by shanazzle
    If i had a list of lists, for example:

    list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

    how can i write a function that only writes out index[1] of each list in the list?

    i need the output to contain 3,5,4,2,2

    and then my final goal is to create a loop that goes through the major list and only print out index[0] of the minor lists if they are not in index[1], so in this case, 4 and 9. I tried using a loop in a loop, but its not working. any ideas?
    [CODE=python]
    >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
    >>> print [item[1] for item in aList]
    [3, 5, 5, 2, 2]
    >>> [/CODE]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      [CODE=python]
      >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
      >>> print [item[1] for item in aList]
      [3, 5, 5, 2, 2]
      >>> [/CODE]
      This will strip the braces off the ends:[CODE=python]
      >>> print str([item[1] for item in aList])[1:-1]
      3, 5, 5, 2, 2
      >>> [/CODE]

      Comment

      • shanazzle
        New Member
        • Oct 2007
        • 10

        #4
        but then could i actually put that output into a list? like, have all the index[1]'s in a list so that i could use it to search back through and remove any index[0] values that are also in index[1]?

        Comment

        • dazzler
          New Member
          • Nov 2007
          • 75

          #5
          Originally posted by shanazzle
          If i had a list of lists, for example:

          list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

          how can i write a function that only writes out index[1] of each list in the list?
          my version ^_^

          do not use "list" as your list's variable name

          so, in your example
          list[0] = [2,3]
          list[1] = [4,5] ....
          so:
          list[0][0] = 2
          list[0][1] = 3
          list[1][0] = 4
          list[1][1] = 5

          the loop:
          [code=python]

          a_list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

          index = 0
          while index != len(a_list): #len(a_list) lenght of the list
          print a_list[index][1] #prints a_list[0][1],[1][1],[2][1],etc...
          index += 1
          [/code]

          Comment

          • dazzler
            New Member
            • Nov 2007
            • 75

            #6
            Originally posted by shanazzle
            list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]

            and then my final goal is to create a loop that goes through the major list and only print out index[0] of the minor lists if they are not in index[1], so in this case, 4 and 9. I tried using a loop in a loop, but its not working. any ideas?
            I don't understand this one, how you get 4 & 9 ? uhm, can you explain more please =)

            Comment

            • shanazzle
              New Member
              • Oct 2007
              • 10

              #7
              Originally posted by dazzler
              I don't understand this one, how you get 4 & 9 ? uhm, can you explain more please =)

              because 4 and 9 are the only numbers that appear in index[0] and do not appear in index[1], does that make sense?

              Comment

              • dazzler
                New Member
                • Nov 2007
                • 75

                #8
                oh, it is searching the whole list for duplicates, for each index[0] values, now I understand =D

                here's the code =)
                I have used index[0] & index[1] names in comments to make it maybe a little clearer for you

                [code=python]
                list_a = [[2,3],[4,5],[2,5],[9,2],[3,2]]
                new_list = []

                index1 =0
                while index1 != len(list_a): #this is looping "index[0]" values

                index2 = 0
                samevalue = 0 #"reseting" this value for the next round
                while index2 != len(list_a): #this is looping "index[1]" values

                if list_a[index1][0] == list_a[index2][1]: #checking if we have same number in "index[0]" & "index[1]"
                samevalue = 1 #let's remember that there were same number

                index2 += 1 #let's try different "index[1]" value

                if samevalue == 0: #if we didn't find pair for the number, samevalue is still 0
                new_list.append (list_a[index1][0]) # .append saves the value to new list (adds it to the end of list)

                index1 += 1 #trying the next "index[0]"

                print new_list
                [/code]

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  This method uses list comprehensions:[code=Python]>>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
                  >>> prntList = [item[1] for item in aList]
                  >>> print ", ".join([str(item) for item in prntList])
                  3, 5, 5, 2, 2
                  >>> print ", ".join([str(item[0]) for item in aList if item[0] not in prntList])
                  4, 9
                  >>> [/code]

                  Comment

                  • ghostdog74
                    Recognized Expert Contributor
                    • Apr 2006
                    • 511

                    #10
                    unorthodox way:
                    Code:
                    >>> str(list).replace("[","").replace("]","").split(",")[1::2]
                    [' 3', ' 5', ' 5', ' 2', ' 2']

                    Comment

                    Working...