Python Forum
Printing through list.. again
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing through list.. again
#1
Question 
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.
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
@menator01 Thank you. I thought for sure I tried that. I see it clearly now. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help for list printing jip31 8 3,664 May-01-2021, 03:52 AM
Last Post: Pedroski55
  Problem printing last element from a list tester_V 3 2,417 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Printing empty list? hhydration 2 2,136 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Printing images from a list Heyjoe 4 2,832 Jun-22-2020, 02:28 AM
Last Post: Heyjoe
  printing a list contents without brackets in a print statement paracelx 1 2,146 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Printing List in one line bharat_s579 6 4,156 May-26-2019, 08:30 PM
Last Post: perfringo
  list is printing incorrectly.. anna 1 2,097 May-18-2019, 12:12 PM
Last Post: ichabod801
  Printing one thing from a list bidoofis 1 2,352 Feb-05-2019, 09:02 PM
Last Post: ichabod801
  Printing a new python list abdallah786 3 88,132 Aug-17-2018, 03:21 PM
Last Post: buran
  [Help] List of Lists not printing both Cmder/Anaconda? vanicci 4 3,576 Aug-15-2018, 03:00 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020