Home > Enterprise >  Python: Append value to new list if it starts with an specific letter and then go to next list and a
Python: Append value to new list if it starts with an specific letter and then go to next list and a

Time:07-03

I am trying to understand how to iterate through two lists look for words that begin with a specific letter and append them to a new list. I was thinking of using for loop and a nested for loop but my code is not working.

list1 = ['iPad','iPhone','macbook','screen','bottel cap','paper','keyboard', 'mouse','bat','Baseball']

list2 = ['couch','tv','remote control', 'play station', 'chair', 'blanket', 'table', 'mug']

list_letter_b = []

for item in list1:
    if item[0] == "b":
        list_letter_b.append(item)
for item in list2:
    if item[0] == "b":
        list_letter_b.append(item)
print("Here is your new list with only items starting with letter b or B {} ".format(list_letter_b))

This code works but it seems too long there has to be a way of making it simpler. Also, I would love to take values that are b and B but cant seems to make it happen.

Any advice is greatly appreciated.

Thank you

CodePudding user response:

I mean, given the context, yours seems fine; however, if you simply want a smaller code footprint, you can do:

list1 = ['iPad','iPhone','macbook','screen','bottel cap','paper','keyboard', 'mouse','bat','Baseball']
list2 = ['couch','tv','remote control', 'play station', 'chair', 'blanket', 'table', 'mug']

list_letter_b = [item for item in list1   list2 if item.lower().startswith("b")]

Note: this is not more "efficient" in any way, just a one line way of doing what you're doing.

CodePudding user response:

Since u also want the values that have an uppercase 'B', you can call .lower() function on the item in the if condition if item[0].lower() == "b":

list1 = ['iPad','iPhone','macbook','screen','bottel cap','paper','keyboard', 'mouse','bat','Baseball']

list2 = ['couch','tv','remote control', 'play station', 'chair', 'blanket', 'table', 'mug']

list_letter_b = []

for item in list1   list2:
    if item[0].lower() == "b":
        list_letter_b.append(item)

print("Here is your new list with only items starting with letter b or B {} ".format(list_letter_b))

CodePudding user response:

Earlier post does help the OP code to be very compact, here is another way - just for simplifying a little, but following OP's orig. logic:

list_letter_b = []


for x in (list1   list2):     # process them as ONE unit/list
    if x.lower().startswith('b'):
        list_letter_b.append(x)
   
        
print("starting with letter b or B {} ".format(list_letter_b))

Output:

starting with letter b or B ['bottel cap', 'bat', 'Baseball', 'blanket']

Or 2nd way to use zip_longest from itertools:

it's because two lists have different size.

from itertools import zip_longest

list_letter_b = []

for x, y in zip_longest(list1, list2, fillvalue=''):
    if x.lower().startswith('b'):
        list_letter_b.append(x)
    
    if y.lower().startswith('b'):
        list_letter_b.append(y)
        
  • Related