0
fruit = ['apple', 'pear', 'peach', 'orange']
fruit1 = ['pear', 'orange', 'banana', 'grapes']
same = []

for f in fruit:
    for ff in fruit1:
        if f == ff:
            same += f
print(same) 

output is:

['p', 'e', 'a', 'r', 'o', 'r', 'a', 'n', 'g', 'e']

I am trying to see if the words are the same and add them in the 'same' list. What is the right code for that?

7 Answers7

3

You can use simple list comprehension like this,

same_fruits = [frt for frt in fruit if frt in fruit1]

You can as well use data structure in Python known as set, If you are aware of set, it allows you to get intersection.

fruit = set(['apple', 'pear', 'peach', 'orange'])
fruit1 = set(['pear', 'orange', 'banana', 'grapes'])
same_fruits = fruit.intersection(fruit1)

Update (Problem in your code)

Your code does not work because, str type itself is a iterable in Python. So, when you use + operator with list and apple or orange, which is again a utterable, Python tries to extend instead of using append method. You could also make a small change in your code, to make sure that str is appended as a whole instead of treating it as individual components by exclusively using append method. Instead of + operator which in this case is extending iterable with another iterable , use append method.

fruit = ['apple', 'pear', 'peach', 'orange']
fruit1 = ['pear', 'orange', 'banana', 'grapes']
same = []

for f in fruit:
    for ff in fruit1:
        if f == ff:
            same.append(f)
print(same) 
Sandeep
  • 20,908
  • 7
  • 66
  • 106
2

try below one line code

same = list(set(fruit).intersection(set(fruit1)))
print(same) 

['orange', 'pear']

1

If the lists are the same lengths you can loop over just one list and compare the elements of each list and append them to the same list.

When adding to a list it is also important to use .append() as right now you're using += which like mentioned in the comments uses .extend.

fruit = ['apple', 'pear', 'peach', 'orange']
fruit1 = ['pear', 'orange', 'banana', 'grapes']
same = []

for f in fruit:
    if f in fruit1:
        same.append(f)

print(same)

Output:

['pear', 'orange']
Jacques
  • 927
  • 9
  • 18
1

I believe what you are looking for is this:

fruit = ['apple', 'pear', 'peach', 'orange']
fruit1 = ['pear', 'orange', 'banana', 'grapes']
same = []

for f in fruit:
    for ff in fruit1:
        if f == ff:
            same.append(f)
print(same)

A better approach would be:

fruit = ['apple', 'pear', 'peach', 'orange']
fruit1 = ['pear', 'orange', 'banana', 'grapes']
same = []

for f in fruit:
    if f in fruit1:
        same.append(f)

print(same)

as this is much cleaner

Spounka
  • 444
  • 6
  • 12
0

This can be done with List comprehension :

fruit = ['apple', 'pear', 'peach', 'orange']
fruit1 = ['pear', 'orange', 'banana', 'grapes']
same = [f for f in fruit if f in fruit1]

you have logic errors in your code below:

for f in fruit:
    if f in fruit1 and f in fruit:
        same.append(f) 

the question and f in fruit in the if statement is incorrect and redundant, because your for is runining on fruits items, so f is an item in fruit thats for sure. how you fix it? just omit this question.

for f in fruit:
    if f in fruit1:
        same.append(f) 
Adam
  • 2,820
  • 1
  • 13
  • 33
0

For your general understanding. You have several list operations like append(), extend(), remove() etc

for f1 in fruit:
    for f2 in fruit1:
        if f1==f2:
            same.append(f1)
print(same)
Hemant Sah
  • 59
  • 1
  • 10
-1
for f in fruit
    if f in fruit1:
        same.append(f)
print(same)
Ryan M
  • 18,333
  • 31
  • 67
  • 74