3

Sorry if the title is not worded properly. First year student currently doing a unit that focuses on the concepts of Python.

How can I call specific elements (if thats the term for it) within a list with a function definition, for_each loop, function definition and if-else-elif statements.

Say this is my list:

data_sets = [
    ['0', ['1', '2', '3']],
    ['4', ['5', '6', '7']]]

And say this is my function definition:

def example(dataset):
    for element in dataset:
        if '1' in dataset:
            return 'Yeah!'
        else:
            return 'Nope!'

I do not understand why it does not return 'Yeah!' when I type data_sets[0]. I know that data_sets[0][1] does return 'Yeah!' but i want to use only one square bracket ([0]) and not two ([0][1]) to return a 'Yeah!'

example(data_sets[0])
#'Nope!'

I am meant to do an exercise that uses different numbers, etc., and it says I should be able to do that with one square bracket ([0]), but I dont know how. We aren't told what to do for the function definition, for each loop, and if-else statements, but we are given the list, and we are expected to do a similar thing with one square bracket. So that may mean I might be doing the function definition, for each loop and if-else statements wrong.

Apologies if I have used incorrect terms or jargon as I am only a beginner.

DYZ
  • 55,249
  • 10
  • 64
  • 93
navre3
  • 35
  • 3

4 Answers4

0

List ['0', ['1', '2', '3']] consists of two elements: '0' and ['1', '2', '3']. Neither of them is '1'. So, '1' is not in data_sets[0]. However, ['1', '2', '3'] has three elements, and one of them is '1'. So, '1' is in data_sets[0][1]. You can first flatten the list (as explained in Making a flat list out of list of lists in Python) and then check if '1' is in the flattened list.

DYZ
  • 55,249
  • 10
  • 64
  • 93
0

['0', ['1', '2', '3']] has a string and a list. You wanted to check if the string '1' was in the list, however, the only string in the list is '0'. This is why it returns nope. Neither of the elements are '1'

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
0

try this , first you need to flatten your list with list comprehension and then check

def example(dataset):
    dataset = [item for sublist in dataset for item in sublist]
    for element in dataset:
        if '1' in dataset:
            return 'Yeah!'
        else:
            return 'Nope!'

and now when you try you code : here is the output :

In [18]: example(dataset[0])
Out[18]: 'Yeah!'
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
0

You could use any to check if any element in the dataset contains 1:

def example(dataset):
    for element in dataset:
        if any('1' in x for x in dataset):
            return 'Yeah!'
        else:
            return 'Nope!'

data_sets = [
    ['0', ['1', '2', '3']],
    ['4', ['5', '6', '7']]]

print(example(data_sets[0]))
# Yeah!

It works because Python strings are iterables. Note that example('123') would also return Yeah!.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124