+ 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?

11 Answers

+ 5

list[1][2:4]

+ 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)

+ 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

+ 1

+ 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?

+ 1

Abhay thank you

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

0

Kirill Vidov so you want those string numbers as well or just ignore them ?

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"?

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

Hot today

I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.

2 Votes

Python palindrome challenge.

1 Votes

Java

0 Votes

Number of Ones ( C++ ) question!

1 Votes