Printing A Specific Item In A Python List
I'm little bit new to the python and I just want to know how to print a single element in a list. as example below is simple list. test_list = [['abc','2'],['cds','333']] I want
Solution 1:
test_list
is a list that contains lists.
Python list
allows random access, i.e. you can access any element you want, providing you know its positional index.
For instance, the first element on the list is test_list[0]
, and the second is test_list[1]
.
In the same manner, the last element is test_list[-1]
, and the second-to-last element is test_list[-2]
.
In your example, you want the first element of the first list, so you can do:
first_list = test_list[0]
first_element = first_list[0]
print(first_element)
Or alternatively, you can get rid of all those temporary variables:
print(test_list[0][0])
Post a Comment for "Printing A Specific Item In A Python List"