0

How to print the even array numbers only in a list?

My_list =[array[1, 2 ,3], array[4,5,6], array[8,9,10], array[11,12,13], array[0,5,66], array[9,19,21]] I want my output to be the following: array[4,5,6] array[11,12,13] array[9,19,21]

27th Jul 2021, 8:52 AM
Karzan
12 odpowiedzi
+ 5
for i in range(len(My_list)) : if i%2!=0: print(My_list[i])
27th Jul 2021, 9:03 AM
Abhay
Abhay - avatar
+ 4
for the sake of correctness: (1) the code as mentioned in the initial post does not work. to use the python "array" class, the module "array" has to be imported. (BTW - i can not remember that i have seen codes here using the "array" class in python. it has a limited set ot methods compared to the "list" class, and if speed matters i would prefer to use numpy) (2) to create an array the syntax has to be: My_list = [array('i',[1,2,3]), array(.....)], in which 'i' indicates the data type of the array to be created to access the list of arrays, a for loop can be used as mentioned by Abhay, also indexing and slicing is possible (3) i assume that the creator of the post wanted to print out all arrays at an even index position. as 0 is the first index, and it is an even number, the output should be a bit different
27th Jul 2021, 10:55 AM
Lothar
Lothar - avatar
+ 3
Use the boolean comparison ((int)arr[i]%2==0), if true print number else continue...works in all languages
28th Jul 2021, 5:59 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Anyway, where's your attempted code?
27th Jul 2021, 9:02 AM
minirkk
minirkk - avatar
+ 2
Karzan Here's a possible solution: for i in range(1, len(My_list), 2): print(My_list[i]) # Hope this helps
27th Jul 2021, 6:27 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Calvin Thomas and others. Yes, thank you guys for helping. Your suggestions was valuable to solve my problem.
27th Jul 2021, 6:51 PM
Karzan
0
Martin Taylor can you share an example please?
27th Jul 2021, 8:57 AM
Karzan
0
RKK your code prints everything in the list. That is not what I am looking for.
27th Jul 2021, 9:00 AM
Karzan
0
RKK I am running on my PC. How can I share what I am seeing? Can I share photos here?
27th Jul 2021, 9:03 AM
Karzan