Question

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

Was it helpful?

Solution

Just do:

lst[0]

to get the first element since list indices start at 0

Examples

>>> a = [1,2,3]
>>> a[0]
1
>>> a[2]
3

Your case

>>> 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'

Extension

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top