+ 2

Python lists

if I have a list: [[1, 2, 3, 4, 10], [5, 6, 7, 8, 9]] how would I get all the numbers between 6 and 9?

15th Feb 2021, 2:13 PM
Kirill
Kirill - avatar
11 Answers
+ 5
list[1][2:4]
15th Feb 2021, 2:15 PM
Abhay
Abhay - avatar
+ 3
Kirill Vidov a=[["a", "f", "c"],["d", "e", "f", "g"]] b=[j for i in a for j in i if ord('d')<=ord(j)<=ord('g')] print(b)
15th Feb 2021, 3:11 PM
Abhay
Abhay - avatar
+ 2
Between here is inclusive, or exclusive? Follow up on Abhay's answer print( [[1, 2, 3, 4, 10], [5, 6, 7, 8, 9]] [1][1: -1] ) # 6, 7, 8 print( [[1, 2, 3, 4, 10], [5, 6, 7, 8, 9]] [1][1: ] ) # 6, 7, 8, 9
15th Feb 2021, 2:19 PM
Ipang
+ 2
thanks Abhay Ipang
15th Feb 2021, 2:23 PM
Kirill
Kirill - avatar
+ 1
Abhay Ipang Jan Markus what if the elements inside the lists are not numbers?
15th Feb 2021, 2:48 PM
Kirill
Kirill - avatar
+ 1
Kirill Vidov After seeing Jan Markus answer i am wondering if that's what you wanted or just the numbers between 6 and 9 from the list like, If it was [[1, 2,3,7,10], [5, 6,7,8,9]] then according to you output should have been 7,6,7,8,9?
15th Feb 2021, 2:52 PM
Abhay
Abhay - avatar
+ 1
Abhay thank you
15th Feb 2021, 3:22 PM
Kirill
Kirill - avatar
0
Abhay yes, I wanted just the the numbers between 6 and 9, but what if the elements inside the lists are not numbers but strings
15th Feb 2021, 2:54 PM
Kirill
Kirill - avatar
0
Kirill Vidov so you want those string numbers as well or just ignore them ?
15th Feb 2021, 2:59 PM
Abhay
Abhay - avatar
0
Abhay no I mean, if the list was instead this: [["a", "b", "c"] ["d", "e", "f", "g"]] how would I get elements between "d" and "g"?
15th Feb 2021, 3:02 PM
Kirill
Kirill - avatar
0
x= [[1, 2, 3, 4, 10], [5, 6, 7, 8, 9]] y= [i for j in a for i in j if 6<=i<=9] print(y); Here you can also refer more operation on it https://www.studytonight.com/python/lists-in-python
16th Feb 2021, 11:35 AM
Sagar Maurya
Sagar Maurya - avatar