0

I'm wondering how to print the first n lines of string in python here is my code:

for word, numbers in enumerate(sorted):
        print(word,':',numbers)

The output looks like following. how can I print just first 10 lines of these string? I've tried to use range, but it only prints True or False...

0 : ('the', 1577)
1 : ('and', 1080)
2 : ('of', 761)
3 : ('to', 734)
4 : ('a', 720)
5 : ('in', 550)
6 : ('it', 451)
7 : ('was', 432)
8 : ('his', 405)
9 : ('he', 381)
10 : ('I', 365)
11 : ('Scrooge', 361)
12 : ('that', 346)
13 : ('with', 312)
14 : ('you', 254)
15 : ('as', 229)
16 : ('said', 221)
17 : ('for', 209)
18 : ('had', 207)
19 : ('him', 198)
20 : ('not', 193)
4
  • 3
    Where is the string.?. Show us the string or the value in variable sorted Commented Oct 25, 2018 at 5:55
  • Are you saying that you want to get the output 0 : ('the', 1577) 1 : ('and', 1080) 2 : ('of', 761) 3 : ('to', 734) 4 : ('a', 720) 5 : ('in', 550) 6 : ('it', 451) 7 : ('was', 432) 8 : ('his', 405) 9 : ('he', 381) .? Commented Oct 25, 2018 at 5:56
  • @SreeramTP Yes!! Commented Oct 25, 2018 at 6:01
  • @SreeramTP The variable sorted looks like [('the', 1577), ('and', 1080), ('of', 761), ('to', 734), ('a', 720), ('in', 550), ('it', 451), ('was', 432), ('his', 405), ('he', 381), ('I', 365), ('Scrooge', 361), ...] Commented Oct 25, 2018 at 6:04

5 Answers 5

15

For those who came to this post for the title and not the OP's question (which is not so related to the question).

For a one-liner we could split() the string by newlines and then join() first N elements of the resulting list by newlines again.

os.linesep.join(str.split(os.linesep)[:10])
Sign up to request clarification or add additional context in comments.

Comments

2

You can use an if statement to break the loop once word, your counter variable as generated by enumerate, reaches 10:

for word, numbers in enumerate(sorted):
    if word == 10:
        break
    print(word,':',numbers)

Comments

1

As mentioned in the comment that you need the output

0 : ('the', 1577) 1 : ('and', 1080) 2 : ('of', 761) 3 : ('to', 734) 4 : ('a', 720) 5 : ('in', 550) 6 : ('it', 451) 7 : ('was', 432) 8 : ('his', 405) 9 : ('he', 381)

You can break the loop after 10 iterations like this.

count = 0
for word, numbers in enumerate(sorted):
    if count == 10:
        break
    print(word,':',numbers)
    count += 1

Or

You can make use of the word and break on the base of that like this

for word, numbers in enumerate(sorted):
    if word == 10:
        break
    print(word,':',numbers)

Keep in mind that it is not a best practice to use reserved keywords as variable names. So, you must be using some other variable name instead of sorted

Comments

1

for me it worked with this:

for key, value in(sorted(my_dict.items(), key=itemgetter(0))[:10]):
print(f'{key} : {value}')

i used slices on my sorted method.

Comments

0

If you only want to get the 10 first elements of the list then just slice the list accordingly

for word, numbers in enumerate(sorted[:10]):
        print(word,':',numbers)

1 Comment

Note that slicing a list just for iteration, although concise and readable, is less efficient because slicing needs to create a new list and copy the items to the new list. Use itertools.islice if you want to slice efficiently for the purpose of iteration.

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.