### Thread: Print elements of an array

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
1
Rep Power
0

#### Print elements of an array

I am beginner with python
If I have an array or list of lisits
how can I print the values of the items that represent the "Z" letter
e.g for the list :
list1 = [ [ 1,2,3],[4,5,6],[7,8,9] ]
it will print 1,2,3,5,7,8,9
2. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
May 2009
Posts
509
Rep Power
33
Originally Posted by The Ghost
how can I print the values of the items that represent the "Z" letter
I'm not sure what that means.
e.g for the list :
list1 = [ [ 1,2,3],[4,5,6],[7,8,9] ]
it will print 1,2,3,5,7,8,9
You first flatten the list which can be done with
Code:
```"""------------------------------
itertools chain
---------------------------------
"""
x = [['foo'], ['bar', 'baz'], ['quux'], ("tup_1", "tup_2"), {1:"one", 2:"two"}]
print list(itertools.chain(*x))

"""------------------------------
or recursion
-----------------------------
"""
def flatten(test):
for el in test:
if type(el) is list:
for sub in flatten(el):
yield sub
else:
yield el

print list(flatten([ [ 1,2,3],[4,5,6],[7,8,9] ]))

"""------------------------------------------------------------------
or if you know it will always be a list of lists one level deep
---------------------------------------------------------------------
"""
result = []
x = [ [ 1,2,3],[4,5,6],[7,8,9] ]
for sub in x:
result.extend(sub)
print result```
Last edited by dwblas; June 17th, 2013 at 04:51 PM.
3. Or you can do this ...
Code:
```mylist = [[1,2,3],[4,5,6],[7,8,9]]
s = str(mylist)
s = s.replace('[', '').replace(']', '')
print(s)

''' result ...
1, 2, 3, 4, 5, 6, 7, 8, 9
'''```
4. Took me a while, but I think I now know what the original poster was asking. Consider the following matrix
Code:
```1  2  3
4  5  6
7  8  9```
Now imagine someone drew the letter 'Z' in the matrix. Basically, the code should print out those digits that would form the shape of the letter Z (i.e.)
Code:
```1  2  3
5
7  8  9```
or rather "1,2,3,5,7,8,9".

Let me know if my interpretation of the request is correct.