0

I need to write the body of a Python method that does the following:

1)takes a list, where list[0] is a string and list[1] is either a list which looks the same or None

2)print every string of the list

I have to use the while loop and not use list comprehension or flatten.

def pick_cherries_iter(field):
    """"e.g.
    >>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]]]
    >>> pick_cherries_iter(cherry_field)
    cherry1
    cherry2
    cherry3
    cherry4
    Yay!!!"""

    _______________________
    _______________________
    _______________________
    _______________________
    while _________________:
        _______________________
        _______________________
        _______________________

I know that for the example above I can print cheery1 if I print cherry_field[0] or cherry1 for cherry_field[1][0] or cherry2 for cherry_filed[1][1][0] etc, however I am not sure how to go through these elements using the while loop.

7
  • Is this an assignment that you have been given? Commented Nov 19, 2018 at 13:56
  • Yes, it is an assignment. Commented Nov 19, 2018 at 13:56
  • Possible duplicate of Flatten a list of strings and lists of strings and lists in Python Commented Nov 19, 2018 at 13:59
  • It is not, I should not use flatten. Commented Nov 19, 2018 at 14:01
  • is there a guarantee in the assignment that each list is at most 2 elements long? Commented Nov 19, 2018 at 14:10

2 Answers 2

0

I think this should work for you. Please check it.

Using While Loop:

def pick_cherry(field):
    """"e.g.
    >>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!',None]]]]]
    >>> pick_cherry(cherry_field)
    cherry1
    cherry2
    cherry3
    cherry4
    Yay!!!"""

    while field[1] != None:
        temp = field[0]
        print temp
        field = field[1]
    print field[0]

Using Flatten(and Recursion):

flatten_field = []

def pick_cherry(field):
    if field[1] != None:
        flatten_field.append(field[0])
        pick_cherry(field[1])
    else:
        flatten_field.append(field[0])

def flatten_func(field):
    """"e.g.
    >>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!',None]]]]]
    >>> flatten_func(cherry_field)
    cherry1
    cherry2
    cherry3
    cherry4
    Yay!!!"""

    pick_cherry(field)

    for item in flatten_field:
        print item
Sign up to request clarification or add additional context in comments.

1 Comment

If I had to use flatten (and recursion), how could this be done?
0

I would do this recursively because you have no way of knowing if an element is a list or not.

#!/usr/bin/python -E

cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]]]
def print_list(field):
    i = 0
    list_length = len(field)
    while i < list_length:
     if field[i] is not None and type(field[i]) is not list:
        print(field[i])
     else:
        if field[i] is not None:
           print_list(field[i])
     i += 1
     if i < list_length and type(field[i]) is list:
        print_list(field[i])
        i += 1


def pick_cherries(field):
    if type(field) is list:
       print_list(field)

pick_cherries(cherry_field)

5 Comments

Does your answer include list comprehension?
I just tested on a variety of nested lists, made a couple minor changes, and it seems to work on everything so far. That's the advantage of recursion.
For example, it can also do this list: cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]],'cherry5']
Your answer using recursion is just fine, my question is whether you are using list coprehension or not.
Umm, no. Referring back to the OP's question, I do not see that requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.