How do i modify lists in lists in Python?

python modify list in function
accessing list of lists python
python list replace element at index
python get first element of list of lists
python modify list in-place
how to modify a list in python
python get subset of list based on condition
python list replace

So, i'm trying to make my elements in my lists uppercase, but i can't make the standard something.upper(), i'm guessing because of the lists in the lists. fx:

names = [['henry', 'mike'],'jones', 'kevin',['michael', 'simon']]

How do i do this?

Thank you


Check if the element is of type string of list

for i in names:
    if isinstance(i,list):
        for inner_element in i:
            print(inner_element.upper())
    elif isinstance(i,str): # to handle the case if ints are also present
        print(i.upper())

If you want to replace the values in existing list

for index,i in enumerate(names):
    if isinstance(i,list):
        temp=[]
        for inner_element in i:
            temp.append(inner_element.upper())
        names[index]=temp
    elif isinstance(i,str):
        names[index]=i.upper()

Python - How to change values in a list of lists?, Looking to modify an item within a list in Python? If so, I'll show you the steps to accomplish this goal using a simple example. Lists in Python are mutable. All that means is that after defining a list, it is possible to update the individual items in a list. # Defining a list z = [3, 7, 4, 2] # Update the item at index 1 with the string "fish" z[1] = "fish" print(z)


You can use list comprehensions as follows:

uppercase_names = [ name.upper() if isinstance(name, str) else [n.upper() for n in name if isinstance(n, str)] for name in names ]

Basically, we're using isinstance(name, str) to check if the object is actually a string object.

In case there are integers in the list, you can use this complex comprehension:

uppercase_names = [ name.upper() if isinstance(name, str) else name if isinstance(name, int) else [ n.upper() if isinstance(n, str) else n if isinstance(n, int) else n for n in name ] for name in names ]

Python Lists and List Manipulation, Or change the members of a list. similar to a list and frequently used in Python programs. List is one of the simplest and most important data structures in Python. Lists are enclosed in square brackets [ ] and each item is separated by a comma. Lists are collections of items where each item in the list has an assigned index value. A list is mutable, meaning you can change its contents.


You could try this if the depth of the list is not known upfront.

Input:

names=['jones', 'kevin', ['henry', 37, ['a', 0.69999]], ['michael', True]]

Function:

def recursive_upper(names):
 ret_list=[]
 for x in names:
     if isinstance(x, list):
         ret_list.append(recursive_upper(x))
     elif (isinstance(x, basestring) or isinstance(x, int) or isinstance(x, float) \
           or isinstance(x, long) or isinstance(x, bool) or isinstance(x, complex)):
         ret_list.append(str(x).upper())
 return ret_list

print recursive_func(names)

Output:

['JONES', 'KEVIN', ['HENRY', '37', ['A', '0.69999']], ['MICHAEL', 'TRUE']]

The function simply checks the type and recursively calls itself if type is a list. It continues to return the uppercase version of text when it finds a string, int, float, long, bool or a complex type. All other types are simply ignored. (You could add/delete types in the elif condition. Refer here )

Hope this helps :)

How to Modify an Item Within a List in Python, Modifying Lists #. The list type also allows you to assign to individual items or slices, and to delete them. L[i] = obj L  newiters = list(zip(*iters)) # Wrapping in list not needed on Python 2 This will get almost what you want, but not quite, because it will be a list of tuples; if that's okay, you're done, if not, you just tweak it to convert to lists: newiters = list(map(list, zip(*iters))) # Wrapping in list not needed on Python 2


Lists, mutability, and in-place methods, A list is a mutable container. This means that we can add values, delete values, or modify existing values. Python list represents a mathematical concept of a  In Python, data types can be either mutable (changeable) or immutable (unchangable). And while most of the data types we’ve worked with in introductory Python are immutable (including integers, floats, strings, Booleans, and tuples), lists and dictionaries are mutable. That means a global list or dictionary can be changed even when it’s


An Introduction to Python Lists, And while most of the data types we've worked with in introductory Python are immutable (including integers, floats, strings, Booleans, and tuples), lists and  In this post, we will learn about Lists in python. Here we perform the basic operations on list like adding items to a list, appending an item at the end of the list, modify the value of the item in the list, removing an item from the list, removing all the items from the list and some slicing operations.


Python lists - working with lists in Python, List Slices. Slices work on lists just as with strings, and can also be used to change sub-parts of the list. list  This list’s length is 3, not 6, it has sublists. Summary. And with this, we are done with lists in Python. In this article, we talked about Python lists, how to create them, index them, slice them, modify them, add elements, delete elements.