By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

Printing only one index of a list

P: 10
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?
Nov 26 '07 #1
Share this Question
Share on Google+
9 Replies


bartonc
Expert 5K+
P: 6,596
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?
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
  2. >>> print [item[1] for item in aList]
  3. [3, 5, 5, 2, 2]
  4. >>> 
Nov 26 '07 #2

bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
  2. >>> print [item[1] for item in aList]
  3. [3, 5, 5, 2, 2]
  4. >>> 
This will strip the braces off the ends:
Expand|Select|Wrap|Line Numbers
  1. >>> print str([item[1] for item in aList])[1:-1]
  2.  3, 5, 5, 2, 2
  3. >>> 
Nov 26 '07 #3

P: 10
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]?
Nov 26 '07 #4

P: 75
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. a_list = [ [2,3], [4,5], [2,5], [9,2], [3,2]]
  3.  
  4. index = 0
  5. while index != len(a_list):    #len(a_list) lenght of the list
  6.    print a_list[index][1]        #prints a_list[0][1],[1][1],[2][1],etc...
  7.    index += 1
  8.  
Nov 26 '07 #5

P: 75
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 =)
Nov 26 '07 #6

P: 10
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?
Nov 26 '07 #7

P: 75
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

Expand|Select|Wrap|Line Numbers
  1. list_a = [[2,3],[4,5],[2,5],[9,2],[3,2]]
  2. new_list = []
  3.  
  4. index1 =0
  5. while index1 != len(list_a): #this is looping "index[0]" values
  6.  
  7.    index2 = 0
  8.    samevalue = 0 #"reseting" this value for the next round
  9.    while index2 != len(list_a): #this is looping "index[1]" values
  10.  
  11.       if list_a[index1][0] == list_a[index2][1]: #checking if we have same number in "index[0]" & "index[1]"
  12.          samevalue = 1 #let's remember that there were same number
  13.  
  14.       index2 += 1 #let's try different "index[1]" value
  15.  
  16.    if samevalue == 0: #if we didn't find pair for the number, samevalue is still 0
  17.       new_list.append(list_a[index1][0]) # .append saves the value to new list (adds it to the end of list)
  18.  
  19.    index1 += 1 #trying the next "index[0]"
  20.  
  21. print new_list
  22.  
Nov 27 '07 #8

bvdet
Expert Mod 2.5K+
P: 2,851
This method uses list comprehensions:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [[2,3], [4,5], [2,5], [9,2], [3,2]]
  2. >>> prntList = [item[1] for item in aList]
  3. >>> print ", ".join([str(item) for item in prntList])
  4. 3, 5, 5, 2, 2
  5. >>> print ", ".join([str(item[0]) for item in aList if item[0] not in prntList])
  6. 4, 9
  7. >>> 
Nov 27 '07 #9

Expert 100+
P: 511
unorthodox way:
Expand|Select|Wrap|Line Numbers
  1. >>> str(list).replace("[","").replace("]","").split(",")[1::2]
  2. [' 3', ' 5', ' 5', ' 2', ' 2']
  3.  
Nov 28 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.