Burak Baris
Burak Baris

Reputation: 31

Python list concatenation logic

I was trying to concatenate two lists, which are 'Names' and 'Ages'. But I wanted to do that with appending their index of [i+1] each time to another list. So instead of ['John', '17', 'Mike', '21'], My goal was that each pair has a different index, and were a list element.Like that --> [['John', '17'], ['Mike', '21']]

(Note: I know I can do that with zip() function, this is for practice)

So I ended up with that code -->

names = ['Ana', 'John', 'Bob', 'Mike', 'July']
ages = ['17', '22', '33', '8', '76']
a = []
b = []
for i in range(len(names)):
    a.append(names[i])
    a.append(ages[i])
    b.append([] + a)
    a.clear()
print(b)

Output --> [['Ana', '17'], ['John', '22'], ['Bob', '33'], ['Mike', '8'], ['July', '76']]

So as you can see I managed to do that, but the weird thing is that line b.append([] + a). I got what I want accidently, when I type b.append(a) it returns empty b list. But by following the path in the attached code, I'm accomplishing what I'm trying to do. Can anybody explain why this is working ? I could not catch it.

Thanks in advance.

Upvotes: 0

Views: 100

Answers (6)

Pablo
Pablo

Reputation: 11031

you can use zip and list to write this in a single line of code:

result = list(zip(names, ages))

Upvotes: 0

chitown88
chitown88

Reputation: 28620

I would use zip within a list comprehension:

names = ['Ana', 'John', 'Bob', 'Mike', 'July']
ages = ['17', '22', '33', '8', '76']

b = [[name,age] for name, age in zip(names,ages)]

Upvotes: 0

Kashif
Kashif

Reputation: 1464

names = ['Ana', 'John', 'Bob', 'Mike', 'July']
ages = ['17', '22', '33', '8', '76']
a = []
b = []
for i in range(len(names)):
    a.append(names[i])
    a.append(ages[i])
    b.append([]+a)
    a.clear()
    
print(b)

According to your code b.append([]+a) it is concatenation every time with array.if you checked with print statement like this

for i in range(len(names)):
    a.append(names[i])
    a.append(ages[i])
    print("=>",a)

then show you output is

=> ['Ana', '17']
=> ['John', '22']
=> ['Bob', '33']
=> ['Mike', '8']
=> ['July', '76']

so that when you add b.append([]+a) we understand more clearly for now b = [] when we try b.append([]+a) it's mean above array concatenate the many arrays into one array .

I think you solve your problem easily when you using zip() for iteration.

myList=[]
for a,b in zip(names,ages):
    list.append([a,b])


print(myList)

output:

[['Ana', '17'], ['John', '22'], ['Bob', '33'], ['Mike', '8'], ['July', '76']]

Upvotes: 0

uditha
uditha

Reputation: 47

Following is my solution using a list comprehension.

names = ['Ana', 'John', 'Bob', 'Mike', 'July']
ages = ['17', '22', '33', '8', '76']

new_list = [ [names[i], ages[i]] for i in range(len(names))]
    
print(new_list)

Upvotes: 0

Z Li
Z Li

Reputation: 4318

Adding prints in the code shows that b gets 'cleared' after the loop, and it was not storing the correct information inside the loop. It is essentially copies of the same a:

names = ['Ana', 'John', 'Bob', 'Mike', 'July']
ages = ['17', '22', '33', '8', '76']
a = []
b = []
for i in range(len(names)):
    a.append(names[i])
    a.append(ages[i])
    print(a)
    b.append(a)
    print(b)
    a.clear()
print(b)
['Ana', '17']
[['Ana', '17']]
['John', '22']
[['John', '22'], ['John', '22']]
['Bob', '33']
[['Bob', '33'], ['Bob', '33'], ['Bob', '33']]
['Mike', '8']
[['Mike', '8'], ['Mike', '8'], ['Mike', '8'], ['Mike', '8']]
['July', '76']
[['July', '76'], ['July', '76'], ['July', '76'], ['July', '76'], ['July', '76']]
[[], [], [], [], []]

This is because lists are mutable in python. When you clear it, the data b is pointing to gets removed as well. When you do []+a, you are creating a new list which is not a reference to a any more. By changing the code this way you can get what you want:

names = ['Ana', 'John', 'Bob', 'Mike', 'July']
ages = ['17', '22', '33', '8', '76']
b = []
for i in range(len(names)):
    a = []
    a.append(names[i])
    a.append(ages[i])
    b.append(a)
print(b)

To help you understand what I mean by mutable, see the following example:

a = ['some data']
b = [a]
print(b)
a.clear()
print(b)
[['some data']]
[[]]

And this is why a+[] works:

a = ['some data']
b = [a+[]]
print(b)
a.clear()
print(b)
[['some data']]
[['some data']]

Upvotes: 2

Amit Kumar
Amit Kumar

Reputation: 633

if both the list have same no of elements then you can use zip() function.

Note: The zip() function will only iterate till the smallest list passed.

list1=[]
for x,y in zip(names,ages):
    list1.append([x,y])
print(list1)

Upvotes: 0

Related Questions