0

I'm still fairly new to Python and I'm learning about nested dictionaries and nested lists and how to extract values from them. I have an object called mylst which has a list of tuples and each tuple has 3 items. Can someone please help explain the following to me so that I understand what is going on with the object test?

Thank you.

test = [fruit for fruit in mylst if "Lemons" in fruit[2]]
  • 1
    Try searching for `list comprehension` in python. What you can find online would be more useful than anyone answering here – mad_ Jul 15 '19 at 20:17

2 Answers2

1

An equivalent using a regular for loop would be the following:

test = []
for data in words:
    if "Greek" in data[2]:
        test.append(data)

Look for python list comprehensions to understand a little more on their behavior.

Jonathan Hamel
  • 1,292
  • 10
  • 18
1

Test is a list of tuples. In test only those tuples are present which is having 'Greek' word in the third item of tuples.

Let's take an example

Words = [(hello everyone, may be, I have new friends), ( hey friends, hii, new Greek meaning) , (how is you, traffic means, Greek means) ]

When we run your statement then it will find only those tuples from the words list which is having Greek present in third element of tuples means

Test =[ ( hey friends, hii, new Greek meaning) , (how is you, traffic means, Greek means) ]

shubham
  • 493
  • 1
  • 12
  • Thank you very much for this explanation. This helps clarify the test object. – EverythingZen Jul 15 '19 at 22:08
  • If it help you to understand clearly then you have to upvote the solution and accept it so that it can be helpful for other peoples who have the same issue. – shubham Jul 16 '19 at 03:53