1

First time asking a question on SO, so please bear with me.

In the python program I'm writing, I am trying to avoid concatenation of a string in a for loop by using the join method (because: efficiency), but I would like for the program to put a count next to each instance.

For example:

[1] First item [2] Second item [3] Third item

From what I understand (I'm not a master pythoner), this form of using .join() is utilizing a list comprehension, and I can't seem to figure out the proper syntax to do what I'm trying to.

temporary_list = ["First item", "Second item", "Third item"]
count = 1
display = "\n".join("> [{}] {}".format((count), t), count += 1 for t in temporary_list[:10])

I could easily do this with a for loop:

    for t in temporary_list[:10]:
            display += "> [{}] {}\n".format(count, t)
            count += 1

I have a similar line of code that works fine without counting.

display = "\n".join("> {}: {}".format(t, display_correctly(number_list[t])) for t in temporary_list[:10])

I've tried playing with the parenthesis, using 'and' and a lot of googling. Is there a way to do this?

If it matters, I'm using python 3.6

3 Answers 3

5

IIUC, you need enumerate:

temporary_list = ["First item", "Second item", "Third item"]
print("\n".join("> [{}] {}".format(n, i) for n, i in enumerate(temporary_list, start=1)))

Output:

> [1] First item
> [2] Second item
> [3] Third item
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly. Enumerate was the missing link. Thank you.
0

Try:

"\n".join("> [{}] {}".format((count), t) for count, t in zip(range(len(temporary_list[:10])), temporary_list[:10]))

My bad, this works now

2 Comments

That was fast. It was missing a couple of parenthesis, but I ran it and got: TypeError: 'list' object cannot be interpreted as an integer
Sorry I fixed the code, but enumerate looks way better I admit.
0

If you just want to print out the index of items in the list. But if you want to count the number of occurences of each item in a list use Counter()

temporary_list = ["First item", "Second item", "Third item"]

for index,value in enumerate(temporary_list):
    print(f'[{index+1}]', value)

OUTPUT:

[1] First item
[2] Second item
[3] Third item

If you want to print out horizontally:

temporary_list = ["First item", "Second item", "Third item"]
print(" ".join([f'[{index+1}] {value}' for index,value in enumerate(temporary_list)]))

OUTPUT:

[1] First item [2] Second item [3] Third item

3 Comments

The counting was strictly for displaying purposes and not stored. Your code answers the question, though, thank you.
Sorry, your title say occurrence count. I was thinking you want to count the occurrences of each items in the list.
I apologize for my limitations in figuring out a clearer wording. The method this code is built into has a user-defined list. If that list has more than 10 items, it's broken into different pages. I thought it would look nicer to have an index alongside each entry (and if it goes to the second page, the first item would be 1 again instead of 11). That's what I meant by counting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.