How to take list input in Python in single line | Example code

文章推薦指數: 80 %
投票人數:10人

To take list input in Python in a single line use input() function and split() function. Where input() function accepts a string, integer, ... HowtotakelistinputinPythoninsingleline|ExamplecodebyRohitDecember11,2021December11,2021 TotakelistinputinPythoninasinglelineuseinput()functionandsplit()function.Whereinput()functionacceptsastring,integer,andcharacterinputfromauserandsplit()functiontosplitaninputstringbyspace. ExampletakelistinputinPythoninasingleline Simpleexamplecode. lst=input('Typeseparatedbyspace:').split() #printlist print('list:',lst) Output: Howtoreadanarrayofintegersfromasinglelineofinputinpython3? Answer:Usemap()functionalongwithinputandsplitfunction.UsinginttoReadanarrayofintegersonly. Thefollowingsnippetwillmapthesinglelineinputseparatedbywhitespaceintoalistofintegers. lst=list(map(int,input("Typenumberwithspace:").split())) #printlist print('list:',lst) Output: Typenumberwithspace:123list:[1,2,3] DocommentifyouhaveanydoubtsorsuggestionsonthisPythonlistinputtopic Note:IDE: PyCharm 2021.3(CommunityEdition)Windows10Python3.10.1All PythonExamples arein Python 3,soMaybeitsdifferentfrompython2orupgradedversions. RohitDegreeinComputerScienceandEngineer:AppDeveloperandhasmultipleProgramminglanguagesexperience.Enthusiasmfortechnology&likelearningtechnical. Sharethis:FacebookWhatsAppLinkedInMoreTwitterPrintRedditTumblrPinterestPocketTelegramSkypeEmail Related Tags:PythoninputPythonList LeaveaReplyCancelreplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Name* Email* Website Comment*Notifymeoffollow-upcommentsbyemail.Notifymeofnewpostsbyemail. Δ ThissiteusesAkismettoreducespam.Learnhowyourcommentdataisprocessed. SearchTopics Searchfor... RecentPosts javascriptvoid(0)inhref VoidfunctionreturnvalueisusedinJavaScript JavaScriptvoidoperator FormactionJavaScriptvoid FormactionJavaScript|attribute Categories Android AndroidInterviewQuestions AndroidKotlin AndroidUI C CSS Datastructure HTML Info Java JavaScript Kotlin MaterialDesign Memes Python Tool TypeScript Archives Archives SelectMonth August2022 July2022 June2022 May2022 April2022 March2022 February2022 January2022 December2021 November2021 October2021 September2021 August2021 July2021 June2021 May2021 April2021 March2021 February2021 January2021 December2020 November2020 October2020 September2020 August2020 July2020 June2020 May2020 April2020 March2020 February2020 January2020 December2019 November2019 October2019 September2019 August2019 July2019 June2019 May2019 April2019 March2019 February2019 January2019 December2018 November2018 October2018 September2018 August2018 July2018 June2018 May2018 September2016 August2016 April2016 March2016 December2015 Subscribe EmailAddress Subscribe   LoadingComments...   WriteaComment... Email(Required) Name(Required) Website



請為這篇文章評分?