for example if i wanted to add a list to another list, like:
a = [a,b,c]
b = [1,2,3]
my code:

def add(a, b):

    a = a.append(b)

it would output ]
what do i have to change to make it output without using the .extend() method?

---
how can i create a function that can split a list into two lists with like equal elements in each list, which in the end both lists together would have all the elements in the original list?
like:
original = [1,2,3,4,5,6]

it would output something like
[1,2,3] [4,5,6]

def half(l):
    

    a = []
    b = []

    for i in range(len(l)/2):
        a = a + l[i]

    for i in range(len(l)/2 - 1):
        b = b+ l[i]


    return a, b

I know it's something wrong with my for loops, the range?

---

Also just wondering is there any way to sort a list without using the .sort method?
like
[1,2,4,3,2]
will output
[1,2,2,3,4]

Thanks for any hints/help.

Recommended Answers

All 6 Replies

When you use a.extend(b) or a.sort() then list a is changed, since both are inplace functions. Also when you pass a list to a function argument, be aware that the list is a mutable object and changes of a mutable object within the function will feed back. The reason is that with mutable objects the address is passed rather than the value. This often surprises beginning programmers.

Adding two lists without changing any of the original list ...

def add_lists(a, b):
    """add lists a and b without changing list a"""
    # make a true copy of a to avoid any chance of feedback
    t = list(a)
    return t + b

a = ['a', 'b', 'c']
b = [1, 2, 3]
c = add_lists(a, b)

print( c )  # ['a', 'b', 'c', 1, 2, 3]

Split a list in half ...

def half_list(a):
    """split a list in half using slicing"""
    h = len(a)//2
    return a[:h], a[h:]

a = [1, 2, 3, 4, 5, 6]
b, c = half_list(a)

print( b )  # [1, 2, 3]
print( c )  # [4, 5, 6]

The function sorted() was introduced with Python24 and does not sort inplace ...

arr = [1, 2, 4, 3, 2]

# sort arr without changing arr
b = sorted(arr)

print( b )  # [1, 2, 2, 3, 4]
Member Avatar for masterofpuppets

hi,
here's a function to split the list in two equal halfs:

def half( l ):
    mid = len( l ) / 2
    a = l[ :mid ]
    b = l[ mid: ]
    
    return a, b

>>> half( [ 1, 2, 3, 4, 5, 6 ] )
([1, 2, 3], [4, 5, 6])
>>>
Member Avatar for masterofpuppets

Adding two lists without changing any of the original list ...

def add_lists(a, b):
    """add lists a and b without changing list a"""
    # make a true copy of a
    t = list(a)
    return t + b

a = ['a', 'b', 'c']
b = [1, 2, 3]
c = add_lists(a, b)

print( c )  # ['a', 'b', 'c', 1, 2, 3]

Split a list in half ...

def half_list(a):
    """split a list in half using slicing"""
    h = len(a)//2
    return a[:h], a[h:]

a = [1, 2, 3, 4, 5, 6]
b, c = half_list(a)

print( b )  # [1, 2, 3]
print( c )  # [4, 5, 6]

:D sorry dude I was typing while you replied...

:D sorry dude I was typing while you replied...

No need to be sorry, that kind of thing happens easily on any forum.

Vagaseat, I tested your code and actually you can just add the two lists this way ...

def add_lists(a, b):
    """add lists a and b without changing list a"""
    return a + b

a = ['a', 'b', 'c']
b = [1, 2, 3]
c = add_lists(a, b)

print( a )  # ['a', 'b', 'c']
print( b )  # [1, 2, 3]
print( c )  # ['a', 'b', 'c', 1, 2, 3]

Very similar to ruby code ...

def add_lists(a, b)
    return a + b
end

a = ['a', 'b', 'c']
b = [1, 2, 3]
c = add_lists(a, b)

p a  # ["a", "b", "c"]
p b  # [1, 2, 3]
p c  # ["a", "b", "c", 1, 2, 3]

Thanks a lot everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.