Jump to content

How to dynamically add items to a list in Python based on a condition?


SubhanUllah

Recommended Posts

my_list = ["A","B","C","Dummy"] 
junk_list = [] 
while True: 
    new_value = input("Enter a value 'quit' to end:") 
    if new_value == "quit": 
        break 
    if new_value.isalpha(): 
        my_list.append(new_value) 
    else: 
        junk_list.append(new_value) 
print(f"Alpha list is: {my_list}") 
print(f"Junk List is: {junk_list}") 

Output


Enter a value 'quit' to end:dddd 
Enter a value 'quit' to end:tttt 
Enter a value 'quit' to end:555 
Enter a value 'quit' to end:@#44 
Enter a value 'quit' to end:quit 
Alpha list is: ['A', 'B', 'C', 'Dummy', 'dddd', 'tttt'] 
Junk List is: ['555', '@#44'] 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...