473,218 Members | 1,452 Online

# 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?
Nov 26 '07 #1
9 1840
bartonc
6,596 Expert 4TB
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
6,596 Expert 4TB
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
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
dazzler
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

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
dazzler
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
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
dazzler
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
2,851 Expert Mod 2GB
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
ghostdog74
511 Expert 256MB
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