I have this list: [(hello(21,15),'now')] A tuple inside a list, and I want to extract the tuple that way the output is
(hello(21,15,'now')
Just do:
lst[0]
to get the first element since list indices start at 0
>>> a = [1,2,3]
>>> a[0]
1
>>> a[2]
3
>>> a = [(hello(21,15),'now')]
>>> a[0]
(hello(21,15), 'now')
If you further want to get the 'now'
text just do:
>>> a[0][1]
'now'
If you want to get a range of values from a list, you can do use [:]
as follows:
>>> a = [1,2,3,4,5,6,7,8]
>>> a[2:6] #this gets the elements from index 2 to 5
[3,4,5,6]
>>> a[-1] #this gets the last element of the list
8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.