0

I'm writing a function that takes as a parameter a list but returns a copy of the list with following changes: • Strings have all their letters converted to upper-case • Integers and floats have their value increased by 1 • booleans are negated (False becomes True, True becomes False) • Lists are replaced with the word ”List” this function should leave the original input unchanged

This is what I have done so far but I'm not sure how can I add all of these to an empty list, here is my program:

name = [1, 2, "abc123", True, [1, 2, 3]]

new_list = [ ]

for element in name:

    if(type(element) == str):
        for i in element:
            if(i.isalpha()):
                element = element.upper()
        new_list += element
        #print(new_list)
        print(element)

    elif(type(element) == int):
        element = element + 1
        print(element)

    elif(type(element) == bool):
        print(not(element))
    else:
        print("list")
JCOC611
  • 19,111
  • 14
  • 69
  • 90
Syed Naqi
  • 41
  • 4

2 Answers2

2

This is much easier if you make a function to handle individual cases first.

def convert(item):
    if isinstance(item, str):
        return item.upper()
    if isinstance(item, bool):
        return not item
    if isinstance(item, list):
        return "List"
    if isinstance(item, int) or isinstance(item, float):
        return item + 1
    raise ValueError("invalid type: {}".format(type(item)))

Once we have this, we can just apply a map:

map(convert, my_list)

And if you desperately need it to be a list and not just an iterable, convert it to a list:

list(map(convert, my_list))
Pierce Darragh
  • 2,072
  • 2
  • 16
  • 29
1

you were so close and fell down on the last hurdle. Just use append to add to the empty list.

name = [1, 2, "abc123", True, [1, 2, 3]]

new_list = [ ]

for element in name:

    if(type(element) == str):
        for i in element:
            if(i.isalpha()):
                element = element.upper()

        new_list.append(element)

    elif(type(element) == int):
        element = element + 1
        new_list.append(element)

    elif(type(element) == bool):
        new_list.append(not element)
    else:
        new_list.append('list')
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • 2
    I think it's unpythonic to use `type(x) == foo` instead of `isinstance(x, foo)` in this case. See: http://stackoverflow.com/a/1549854/3377150 – Pierce Darragh Nov 03 '16 at 23:34
  • @PierceDarragh that may be so, but the question said only "I'm not sure how can I add all of these to an empty list" and so I made the minimum modifications to make it work with original work to fill in the gap. – roganjosh Nov 03 '16 at 23:36
  • 1
    That's fair, but I think the spirit of this site would suggest that we guide the questioners in finding a better solution than their own, where appropriate. In this case, I think it's worth at least pointing out to OP that `isinstance` is generally preferred over `type` for such things. – Pierce Darragh Nov 03 '16 at 23:42
  • 1
    @PierceDarragh I won't disagree with that. But the OP clearly put effort in and had one missing step. If you want to elaborate then you should at least say what they were missing before jumping forwards. If they missed `append` then how will they understand `map`? – roganjosh Nov 03 '16 at 23:47
  • 1
    That's a really good point, yeah. I guess I was answering the question "How do I do X?" when the intended question was more "What is wrong with my code?" I've given you an upvote, but I'll leave my answer as an alternative solution. :) – Pierce Darragh Nov 03 '16 at 23:49
  • i have another question, instead of making a new list that I'm adding to how can i mutate my original list to do all this – Syed Naqi Nov 04 '16 at 00:25
  • @SyedNaqi Good question :) Stack Overflow operates, almost always, on a single question at a time. Have a go and, if you get stuck, you need to raise as a new question. Make sure you show your efforts moving on from this point. – roganjosh Nov 04 '16 at 00:30
  • I understand that, but the function does the same thing the only difference is that instead of making a copy of the original list we are changing the list itself. I have tried to use some of the list methods but they are not working also I think I can just make my original list empty and than add to it, but that wouldn't be a efficient. – Syed Naqi Nov 04 '16 at 00:35
  • @SyedNaqi again, it's not directly to do with this question so, should you get stuck, it needs to be raised as a new question. You don't make a copy of the list in this code, you build one from scratch (you start with `[]` and `append` to it bit by bit). You might not think it efficient, but I can't think of a more efficient way of modifying your original list to match all your constraints. Just build a new one. – roganjosh Nov 04 '16 at 00:43