Python Forum
[split] question about list comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] question about list comprehension
#7
Quote:if I enter say 10 in this case the string it will be .split () will be output to '1' and '0'
That's not how split works.
split requires a delimiter. If none is supplied space is assumed.
the results of a split are returned in a list

remember, the best way to learn is to try.
bring up the python interpreter so you can try for yourself (just type python3 at command line)
some examples:

example 1, no delimiter specified, so default space is used
>>> string1 = "This is a string"
>>> string1.split()
['This', 'is', 'a', 'string']
example 2 specifing a delimiter:
>>> string2 = "This string has a hyphenated-word in it"
>>> string2.split("-")
['This string has a hyphenated', 'word in it']
>>>
useful for separating items delimited with special characters:
example3:
>>> data = "10/18/2016|ALA|16|970|827|540|2337"
>>> dlist = data.split('|')
>>> dlist
['10/18/2016', 'ALA', '16', '970', '827', '540', '2337']
>>>
Reply


Messages In This Thread
RE: [split] question about list comprehension - by Larz60+ - Jan-12-2020, 11:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 643 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 533 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,355 Apr-02-2023, 06:31 PM
Last Post: tester_V
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,628 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  list comprehension 3lnyn0 4 1,490 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  Split string using variable found in a list japo85 2 1,363 Jul-11-2022, 08:52 AM
Last Post: japo85
  [split] [split] New to the forum, how to post a question? karnik 2 1,321 Feb-12-2022, 03:45 PM
Last Post: deanhystad
  List comprehension used differently coder_sw99 3 1,794 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,951 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,294 Jun-08-2021, 08:29 AM
Last Post: cametan

Forum Jump:

User Panel Messages

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