Numbering loop python
Question:
Please I’m trying to number this loop:
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Referral Listn"
sort = sorted(TopList.items(), key=lambda x: x[1], reverse=True)
for info in sort:
num = 1
userlink = f'<a href="tg://user?id={info[0]}">{info[0]}</a>'
meg += f"n{num} {userlink} - {info[1]}n"
if len(sort) > 10:
break
print(meg)
I want it to look like this:
Top Referral List
1 9864937087 - 8
2 1350180828 - 3
3 1670937087 - 2
4 0743180828 - 1
Answers:
You can do this,
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Refferal Listn"
sorted_keys = sorted(TopList, key=TopList.get, reverse=True)
for idx, key in enumerate(sorted_keys,1):
userlink = f'<a href="tg://user?id={key}">{key}</a>'
meg += f"n{idx} {userlink} - {TopList.get(key)}n"
print(meg)
Few things updates,
- Sorted the keys based on the value
- Iterate through the key and fetch the values during the iteration
- Used
enumerate
for the index values
Output:
Top Refferal List
1 <a href="tg://user?id=9864937087">9864937087</a> - 8
2 <a href="tg://user?id=1350180828">1350180828</a> - 3
3 <a href="tg://user?id=1670937087">1670937087</a> - 2
4 <a href="tg://user?id=0743180828">0743180828</a> - 1
Edit:
An updated version inspired by the comments of Blckknght &
Ben Grossmann.
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Refferal Listn"
sorted_items = sorted(TopList.items(), key=lambda x: x[1], reverse=True)
for idx, (key, value) in enumerate(sorted_items, 1):
userlink = f'<a href="tg://user?id={key}">{key}</a>'
meg += f"n{idx} {userlink} - {value}n"
print(meg)
The reason why it isn’t working is because you never increment the num
variable. So for each iteration it simply outputs a 1 every time. A simple solution would be to move the num
variable outside of the loop and then increment it on each iteration after you have printed.
An alternative method would be to use enumerate
which has already been demonstrated by RahulKP. This is the better option for conciseness and readability. The example below is just to demonstrate how you could make your code work
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Refferal Listn"
sort = sorted(TopList.items(), key=lambda x: x[1], reverse=True)
num = 1
for info in sort:
userlink = f'<a href="tg://user?id={info[0]}">{info[0]}</a>'
meg += f"n{num} {userlink} - {info[1]}n"
num += 1
if len(sort) > 10:
break
print(meg)
Please I’m trying to number this loop:
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Referral Listn"
sort = sorted(TopList.items(), key=lambda x: x[1], reverse=True)
for info in sort:
num = 1
userlink = f'<a href="tg://user?id={info[0]}">{info[0]}</a>'
meg += f"n{num} {userlink} - {info[1]}n"
if len(sort) > 10:
break
print(meg)
I want it to look like this:
Top Referral List
1 9864937087 - 8
2 1350180828 - 3
3 1670937087 - 2
4 0743180828 - 1
You can do this,
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Refferal Listn"
sorted_keys = sorted(TopList, key=TopList.get, reverse=True)
for idx, key in enumerate(sorted_keys,1):
userlink = f'<a href="tg://user?id={key}">{key}</a>'
meg += f"n{idx} {userlink} - {TopList.get(key)}n"
print(meg)
Few things updates,
- Sorted the keys based on the value
- Iterate through the key and fetch the values during the iteration
- Used
enumerate
for the index values
Output:
Top Refferal List
1 <a href="tg://user?id=9864937087">9864937087</a> - 8
2 <a href="tg://user?id=1350180828">1350180828</a> - 3
3 <a href="tg://user?id=1670937087">1670937087</a> - 2
4 <a href="tg://user?id=0743180828">0743180828</a> - 1
Edit:
An updated version inspired by the comments of Blckknght &
Ben Grossmann.
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Refferal Listn"
sorted_items = sorted(TopList.items(), key=lambda x: x[1], reverse=True)
for idx, (key, value) in enumerate(sorted_items, 1):
userlink = f'<a href="tg://user?id={key}">{key}</a>'
meg += f"n{idx} {userlink} - {value}n"
print(meg)
The reason why it isn’t working is because you never increment the num
variable. So for each iteration it simply outputs a 1 every time. A simple solution would be to move the num
variable outside of the loop and then increment it on each iteration after you have printed.
An alternative method would be to use enumerate
which has already been demonstrated by RahulKP. This is the better option for conciseness and readability. The example below is just to demonstrate how you could make your code work
TopList = {'1350180828': 3, '1670937087': 2, '0743180828': 1, '9864937087': 8}
meg = "Top Refferal Listn"
sort = sorted(TopList.items(), key=lambda x: x[1], reverse=True)
num = 1
for info in sort:
userlink = f'<a href="tg://user?id={info[0]}">{info[0]}</a>'
meg += f"n{num} {userlink} - {info[1]}n"
num += 1
if len(sort) > 10:
break
print(meg)