You could just do:

print(friends[1])

That would give the second element, it will output:

Mark

If you want to use a loop, try:

for i, v in enumerate(friends):
    if i == 1:
        print(v)

Output:

Mark

Python indexing starts from 0, so the second element's index would be 1.

The for loop I did iterates through the list, but the iterator i is the index of every element, and v is the value, so it check if i (the index) is 1, if so, it prints v.

Answer from U13-Forward on Stack Overflow
Discussions

In a Python list, how can I print the 1st and 2nd elements on a line, the 3rd and 4th on the next line, and so forth? - Stack Overflow
I am generating a string and appending it to a list. I am looping that action to make a list of 40 elements. I want to print them like so: here's my list list = [ 'first', 'second', 'third', ... ' More on stackoverflow.com
🌐 stackoverflow.com
Python print nth element from list of lists - Stack Overflow
I want to print the third element from every list. More on stackoverflow.com
🌐 stackoverflow.com
How do I access the 2nd item in a list?
Question How do I access the 2nd item in a list? Answer Recall that lists start counting from 0, not 1! So if we want to access an item, we start as 0 for the index of the first item, and count our way up to the item we want. In this case, we’re given a list n = [1, 3, 5], so the second element ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
1
June 21, 2018
python - Printing specific items out of a list - Stack Overflow
I'm wondering how to print specific items from a list e.g. given: li = [1,2,3,4] I want to print just the 3rd and 4th within a loop and I have been trying to use some kind of for-loop like the fol... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bobby Hadz
bobbyhadz.com › blog › python-get-second-element-of-tuple
Get the Nth element of a Tuple or List of Tuples in Python | bobbyhadz
We used an index of 1 to get the second element of a tuple. Python indexes are zero-based, so the first element in a tuple has an index of 0, the second an index of 1, etc. ... Copied!my_tuple = ('bobby', 'hadz', 'com') first_item = my_tuple[0] print(first_item) # 👉️ bobby last_item = my_tuple[-1] print(last_item) # 👉️ com
🌐
Quora
quora.com › How-do-I-print-nth-elements-in-the-list-simultaneously-in-Python
How to print nth elements in the list simultaneously in Python - Quora
Below are patterns and idiomatic Python ways, covering common needs. 1) Basic extraction when you know index n and all sequences have that index ... Print the n-th elements of multiple lists “simultaneously” typically means: for a collection of sequences, extract element at index n from each sequence (handling out-of-range and differing lengths).
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
How do I access the 2nd item in a list? - Python FAQ - Codecademy Forums
June 21, 2018 - Question How do I access the 2nd item in a list? Answer Recall that lists start counting from 0, not 1! So if we want to access an item, we start as 0 for the index of the first item, and count our way up to the item we want.
Find elsewhere
🌐
CodeRivers
coderivers.org › blog › how-to-print-specific-element-of-a-list-in-python
Printing Specific Elements of a List in Python - CodeRivers
April 23, 2025 - If omitted, it defaults to 0. - stop is the index where the slice stops (exclusive). If omitted, it defaults to the length of the list. - step is the number of elements to skip between each element in the slice. If omitted, it defaults to 1. my_list = [10, 20, 30, 40, 50, 60] print(my_list[1:4])
🌐
Bobby Hadz
bobbyhadz.com › blog › python-print-specific-items-in-list
How to Print specific items in a List in Python | bobbyhadz
The slice list_of_lists[0][1] returns the second element of the first sublist. I've also written an article on how to print a list in columns.
🌐
YouTube
youtube.com › watch
How to get every second element from a list in Python - example - YouTube
In this lesson we're going to talk about that how to get every second (every other) element from a list in Python.We're going to use range method with start,...
Published   December 29, 2016
Top answer
1 of 2
2

You missed the brackets. Try this:

p = [x[1] for x in list_of_numbers]

To print the values, you could use

print(', '.join([str(x) for x in p]))

You also need to change the way you load the data from the file

Full Code:

def parse(raw):
    data = []
    for line in raw.split("\n"):
        line = line.strip()
        # --> "('5', 2.5, 5200)"
        if line.startswith("(") and line.endswith(")"):
            d = line[line.index("(")+1 : line.index(")", -1)]
            # --> "'5', 2.5, 5200"
            d = d.split(",")
            data.append([])
            for i in d:
                i = i.strip()
                try:
                    i = float(i)
                except:
                    pass
                data[-1].append(i)
    return data


raw = open("list_of_numbers.txt").read()

list_of_numbers = parse(raw)

p = [x[1] for x in list_of_numbers]
# --> [2.5, 3.2, 5.4, 8.7]
print(', '.join([str(x) for x in p]))
# ---> 2.5, 3.2, 5.4, 8.7

I suggest using pickle. Storing and loading your data is easy as:

import pickle
data = ...
# store
file = open('data.txt', 'w')
pickle.dump(data, file)
file.close()
# load
file = open('data.txt', 'r')
data = pickle.load(file)
file.close()
2 of 2
0

Another option is to use numpy.ndarray.

import numpy as np
list_of_numbers = [
    ('5', 2.5, 5200),
    ('6', 3.2, 5236),
    ('8', 5.4, 5287),
    ]
list_of_numbers = np.array(list_of_numbers)
p = list_of_numbers[:,1]
print(p)
# outputs: ['2.5' '3.2' '5.4']

In addition, since you're reading data from a text file, your first list should contain only str. (I really don’t understand how you get mixed strings and numbers using the method you describe in your question.) To fix that, you can either:

  • use numpy.loadtxt,
  • convert to float when switching to a ndarray: `np.array(list_of_numbers, dtype=float).

Finally, I strongly suggest that you learn about slices in Python.

🌐
FavTutor
favtutor.com › blogs › print-list-python
How to Print a List in Python: 5 Different Ways (with code)
April 13, 2023 - The simplest and standard method to print a list in Python is by using loops such as a 'for' or 'while' loop. Using for loop, you can traverse the list from the 0th index and print all the elements in the sequence.
🌐
Python.org
discuss.python.org › python help
Lists ( how to print specific item ) - Python Help - Discussions on Python.org
September 1, 2024 - How do you print a specific item from a list?? mylist = ["banana", "cherry", "apple"] for i in mylist : print(mylist[0]) When I run the above code it prints… banana banana banana And I don’t know how to fix it…
🌐
ItSolutionstuff
itsolutionstuff.com › post › python-get-second-last-element-of-list-exampleexample.html
Python Get Second Last Element of List Example - ItSolutionstuff.com
October 30, 2023 - Then I will get second last element with name using -2 key of array. so let's see the below example. You can use these examples with python3 (Python 3) version. ... myList = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Get Last Element ...
🌐
Java2Blog
java2blog.com › home › python › python list › get every other element in list in python
Get Every Other Element in List in Python - Java2Blog
October 4, 2022 - The following code uses the enumerate() function along with list comprehension to get every other element in a list in Python. Using the enumerate() function along with list comprehension ... A lambda function is an anonymous function that is capable of holding a single expression while taking ...
Top answer
1 of 2
2

You're getting this error because you try to access the second element of an array that contains only 1 string. In this case you want to check the length of the array

for line in open("testing.txt"):
   strip = line.rstrip()
   words = strip.split(';')
   for test in words:
      if len(words) > 1:
         print(words)
      else: # this else is not necessary
         continue

Edit: If you want to print each sentences containing at least one ';' only once, you don't actually have to use a for loop. One concise way to get the desired output would be this:

for line in open("testing.txt"):
    strip = line.rstrip()
    words = strip.split(';')
    if len(words) > 1:
        print(words)
2 of 2
0

As far as I understand from your question you only trying to print the words list which has more than one element.

One simple way to do it is:

    for line in open("testing.txt"):
       strip = line.rstrip()
       words = strip.split(';')
       # first = words[0]
       for test in words:
          if len(words) > 1:
             print(words)

Here you are just checking if the length of the words is greater than 1 and printing if that is the case

EDIT: I think the for loop is unnecessary. All you want is to print lists of words greater than length 1. So for that purpose:

    for line in open("testing.txt"):
       strip = line.rstrip()
       words = strip.split(';')
       if len(words) > 1:
          print(words)

Here you are just splitting the sentences on ; and then checking after splitting if the length of the list (named words) is greater than 1; if so you are printing the list named words.

EDIT2: As S3DEV had pointed out that you are opening a file inside for keyword which won't close your file automatically once you are out of for loop. As a result the file pointer remains open until the program stopped completely and it might cause weird issues. The best practice is to use with keyword. the with keyword automatically opens the file nad closes it once the block execution is complete, so you won't face any odd issues. form keeping a file pointer open.

    with open("testing.txt", "r") as f: # this line open file as f in read-only format
       for line in f:
          strip = line.rstrip()
          words = strip.split(';')
          if len(words) > 1:
             print(words)