Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with list function
#1
Hello and thank you for looking at my post. I am very new to programing and python and am doing exercises from http://www.practicepython.org/solution/2...tions.html . I am on exercise 3. and I have completed the first task however it is asking for me to convert the result of the if statement back into a list. Here is the code I have so far.

a = [1,1,2,3,5,8,13,21,34,55,89]

for element in a:
    if element <= 4:
        print (element)
I need to figure out how to turn the result into a list. I have tried the list function by doing: print list(element) and that threw a syntax error. I have tried it a few other ways all of which have given me syntax errors. I have watched many list tutorials on YouTube and have come here as a last resort. Please forgive me for any errors in my post format and I appreciate all help very much. P.S I am running Python 3
Reply
#2
First, the error. print() is only used to display information on the standard out, which is the command line or console. It has no application here.

Now, a skill that will greatly assist you when developing is reading the documentation. It's the best place to begin your research into why something isn't working or what you can do with a particular object. The Python documentation for lists mentions an append() method for lists. Methods are basically functions associated with a particular class. They can be called with dot notation (e.g. list.append()). To implement in your code, you'll want to instantiate a list for your output and loop over list.append() to make it work.

a = [1,1,2,3,5,8,13,21,34,55,89]
out = []
 
for element in a:
    if element <= 4:
        out.append(element)

print(out)
Reply
#3
Excellent I am glad I came here. I will refer to documentation and I apologize for my initial error in my posting format. Thank you once again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 384 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Run a Function?: Trouble with Online Python Course webmanoffesto 3 967 Aug-18-2022, 10:14 PM
Last Post: deanhystad
  list trouble rediska 3 2,185 Oct-17-2020, 11:17 AM
Last Post: ibreeden
  Appending list Trouble Big Time Milfredo 7 3,022 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  trouble with list array Milfredo 2 1,989 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Trouble with converting list , dict to int values! faryad13 7 3,668 Sep-04-2020, 06:25 AM
Last Post: faryad13
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,247 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Trouble on importing function from astropy library samsonite 4 3,635 Mar-21-2019, 12:18 PM
Last Post: samsonite
  For loop (adding strings to a list trouble) eoins 2 2,998 Jul-03-2018, 10:36 AM
Last Post: volcano63
  trouble while appending list in for loop py_learner 1 2,815 May-28-2018, 01:55 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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