Python Forum

Full Version: Printing through list.. again
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi simple help needed please. Just trying to print a specific item in the list. If I iterate through the list I can print each value on a new line. If I try to specify a value in the list it prints a character from the last value in the list. This makes me doubt that I am even working with a 'list' at all. How do I address a specific value in this 'list'? And actually I just run Type command it looks like it is in fact a 'class 'list'

my_list = ['-rw-rw-r--', '1', 'root', 'root', '652173', 'Apr', '21', '2020', 'ecs_12032020.cfg']
print (my_list)
print ("------------------------")
for x in my_list:
    print (x)
print ("------------------------") 
print (x[7])
OUTPUT:
Output:
['-rw-rw-r--', '1', 'root', 'root', '652173', 'Apr', '21', '2020', 'ecs_12032020.cfg'] ------------------------ -rw-rw-r-- 1 root root 652173 Apr 21 2020 ecs_12032020.cfg ------------------------ 3
Many Thanks.
my_list = ['-rw-rw-r--', '1', 'root', 'root', '652173', 'Apr', '21', '2020', 'ecs_12032020.cfg']
print (my_list)
print ("------------------------")
for x in my_list:
    print (x)
print ("------------------------") 
print (x[7]) #This should be
print(my_list[7])  #to get this element

Example:
my_list = ['-rw-rw-r--', '1', 'root', 'root', '652173', 'Apr', '21', '2020', 'ecs_12032020.cfg']

print(f'my_list -> {my_list}')
for i, element in enumerate(my_list):
    print(f'Element {i} in my_list -> {my_list[i]}')
Output:
my_list -> ['-rw-rw-r--', '1', 'root', 'root', '652173', 'Apr', '21', '2020', 'ecs_12032020.cfg'] Element 0 in my_list -> -rw-rw-r-- Element 1 in my_list -> 1 Element 2 in my_list -> root Element 3 in my_list -> root Element 4 in my_list -> 652173 Element 5 in my_list -> Apr Element 6 in my_list -> 21 Element 7 in my_list -> 2020 Element 8 in my_list -> ecs_12032020.cfg
@menator01 Thank you. I thought for sure I tried that. I see it clearly now. Thanks