Python Forum
What a difference print() makes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What a difference print() makes
#1
Hi all,

list1 = ["cat", "dog", "cat", "dog"]
list1.index("cat")
list1.count("dog")
If I enter that in a Jupyter Notebook cell then the output is: 2

list1 = ["cat", "dog", "cat", "dog"]
print(list1.index("cat"))
print(list1.count("dog"))
If I enter that in a Jupyter Notebook cell then the output is 0 with 2 on the very next line.

The .index() and .count() functions are being called in both programs but in the first, the second function return replaces the first function return. How would I explain this observation in pythonic terms?

Thanks!
Reply
#2
Great example here:

https://www.dataquest.io/blog/jupyter-no...-tutorial/

Which says:
Quote:In general, the output of a cell comes from any text data specifically printed during the cell's execution, as well as the value of the last line in the cell, be it a lone variable, a function call, or something else.
If you ran this cell:
list1 = ["cat", "dog", "cat", "dog"]
print(list1.index("cat"))
print(list1.count("dog"))
f"It is raining {list1[0]}s and {list[1]}s"
The output would be
Output:
cat dog It is raining cats and dogs
The python interactive interpreter does something similar, but it automaticlly prints output at the end of each line, not each cell.
Output:
>>> print("spam") spam >>> print(["spam"] * 4) ['spam', 'spam', 'spam', 'spam'] >>> ", ".join(["spam"] * 4) 'spam, spam, spam, spam' >>>
Reply
#3
As a side note, you could iterate over the enumerate(list1) and then you get (index, list_element).
list1 = ["cat", "dog", "cat", "dog"]


def get_all_indicies(iterable, item):
    for index, elemnt in enumerate(iterable):
        if item == elemnt:
            yield index


all_indicies = list(get_all_indicies(list1, "cat"))
Otherwise, you can use list1.index("item_to_find", START_INDEX).
list1 = ["cat", "dog", "cat", "dog"]


def get_all_indicies(list_like, item):
    index = 0

    while True:
        try:
            index = list_like.index(item, index)
            yield index
        except ValueError:
            return

        index += 1


list(get_all_indicies(list1, "cat"))
Mark17 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter + Multiprocessing startmap makes UI Freeze sunny9495 4 1,405 Nov-13-2022, 12:49 PM
Last Post: sunny9495
  Upgrade makes Code Error kucingkembar 6 3,118 Jul-28-2022, 06:44 PM
Last Post: kucingkembar
  help! this 'for' loop makes me crazy lerner50 5 2,946 Jul-09-2019, 03:39 PM
Last Post: perfringo
  Difference between return and print DragonG 9 6,577 Oct-26-2018, 12:06 AM
Last Post: Skaperen
  The number of object makes code slower? fig0 1 2,519 Jan-25-2018, 11:16 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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