|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Reading the longest line in a list?
Suppose i want to read and print the longest line out of a list of my items from a txt file without mentioning that specifc line manually. Whats the best code to be suggested to identify the longest line out of that list and print THAT line only.Is there a way or such code in python?
Radio Television Cellphone Washing machine (should print out washing machine as its the longest line here) i know how to put manually the address of the line and read it.But isnt there any code that identifies the longest lenght and prints it out itself?? Thanks |
|
#2
|
||||||
|
||||||
|
Use the max function with the key parameter.
Quote:
for example: python Code:
This will work if lines is any iterable object, including a file. Dave |
|
#3
|
|||
|
|||
|
Quote: Thank you Dave! Just one last thing how can i combine this code with the open(filnename, 'r') code if the list is from a txt file? |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Reading the longest line in a list? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|