Python Forum
Shorten this List Comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shorten this List Comprehension
#7
(Oct-09-2016, 08:25 AM)snippsat Wrote:
(Oct-09-2016, 01:10 AM)ATXpython Wrote: Is it possible to do the above in one list comprehension?
Seems kind of wasteful to create a list based on another list, if all I'm trying to do is remove certain characters from it.
No to much in one list comprehension make it long and harder to read.
So it's not ideal at all.
I think it's okay as you have it now.

There are some different way like translate as mention and i can show one with regex.
>>> import re
>>> lst = ['hello?', 'wo+rld@', 'toge?the]']
>>> [re.sub(r'[?@\]+.,]', '', item) for item in lst]
['hello', 'world', 'together']
Quote:While, I know PEP-8 isn't law, is there any downside to having a line so long in Python?
It's to long when you get over 100 if you ask me,around 90'ish is okay.

it depends on typical terminal size.  even with big screen terminal programs usually default to to 80 for the width.  many do not "fix" wrapping issues so limiting at 79 on these is better.  people often leave term programs at the defaults.

i have changed mine to nearly full screen (166 wide, 46 lines, 14 pt font, on 1920x1080).  but this also limits me from doing 2 term windows side by side.   if i shink 2 term windows they end up nearly at 80 wide.  so, despite some of my code being very wide (i have some that exceeds 1000's) i suggest making code fit in 79 .... not even 90-ish ... just 79.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
Shorten this List Comprehension - by ATXpython - Oct-09-2016, 01:10 AM
RE: Shorten this List Comprehension - by ichabod801 - Oct-09-2016, 01:21 AM
RE: Shorten this List Comprehension - by ATXpython - Oct-09-2016, 01:42 AM
RE: Shorten this List Comprehension - by ichabod801 - Oct-09-2016, 12:55 PM
RE: Shorten this List Comprehension - by snippsat - Oct-09-2016, 08:25 AM
RE: Shorten this List Comprehension - by Skaperen - Oct-10-2016, 07:09 AM
RE: Shorten this List Comprehension - by snippsat - Oct-09-2016, 02:57 PM
RE: Shorten this List Comprehension - by wavic - Oct-10-2016, 07:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  If I need to do regex replacement 27000 times, how to shorten it? tatahuft 4 877 Dec-26-2024, 01:51 AM
Last Post: deanhystad
  List comprehension not working right Cris9855 3 1,016 Nov-04-2024, 03:46 PM
Last Post: DeaD_EyE
  i want to shorten a working section pizzakafz 15 2,954 Aug-23-2024, 11:56 AM
Last Post: deanhystad
  Problem with List Comprehension in Python laurawoods 3 1,177 Aug-12-2024, 06:26 AM
Last Post: Pedroski55
  List Comprehension Issue johnywhy 5 2,013 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 1,711 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 3,694 Apr-02-2023, 06:31 PM
Last Post: tester_V
  python multiple try except block in my code -- can we shorten code mg24 10 15,575 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  list comprehension 3lnyn0 4 2,463 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  Want to shorten the python code shantanu97 3 2,127 Apr-25-2022, 01:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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