Reputation: 213
I have a list of lists containing:
animal = [[1, 'Crocodile','Lion'],[2, 'Eagle','Sparrow'],[3, 'Hippo','Platypus','Deer']]
and I want to join the string elements in each list inside the animal table so that it becomes a single string:
animal = [[1, 'Crocodile, Lion'],[2, 'Eagle, Sparrow'],[3,'Hippo, Platypus, Deer']]
I tried using a for loop to join them:
for i in range(len(animal)):
''.join(animal[1:]) #string at index 1 and so on
print(animal)
I'm getting a type error saying "TypeError: sequence item 0: expected str instance, list found".
Upvotes: 2
Views: 268
Reputation: 51
l1=[[1, 'Crocodile','Lion'],[2, 'Eagle','Sparrow'],[3, 'Hippo','Platypus','Deer']]
l2 = list(map(lambda x: [x[0],"{}, {}".format(x[1],x[2])],l1))
print(l2)
Upvotes: 0
Reputation: 101
Just a small change needed, you just forgot the index i in your loop:
animal = [[1, 'Crocodile','Lion'],[2, 'Eagle','Sparrow'],[3, 'Hippo','Platypus','Deer']]
merged_animal = []
for element in animal:
merged_animal.append([element[0], ", ".join(element[1:])])
print(merged_animal)
But if you know list-comprehensions, better use them as shown in many answers.
Upvotes: 1
Reputation: 1343
Firstly, the type error is coming because you are applying join()
on the list animal not on the sublists of list animal.
You should also keep in mind that join does not edit the original list, it just returns the new string.
Hence if you keep the above two things in mind your new code will look something like this
for i in range(len(animal)):
animal[i] = [animal[i][0], ', '.join(animal[i][1:])] #string at index 1 and so on
print(animal)
the above code replaces each sublist with another sublist containing the sublist number and a string formed by joining the remaining part of original sublist with a ', '
(note your mistake here, you were joining with an empty character but your requirement is a comma and a space).
Upvotes: 0
Reputation: 796
animal
is a list of lists so you need an extra index. You see this by adding
print(animal[i])
print(animal[i][0])
print(animal[i][1])
etc in the loop
animal = [[1, 'Crocodile','Lion'],[2, 'Eagle','Sparrow'],[3, 'Hippo','Platypus','Deer']]
for i in range(len(animal)):
print(animal[i][1:])
animal[i][1] = ' '.join(animal[i][1:]) #string at index 1 and so on
del animal[i][2:]
print(animal)
Upvotes: 0
Reputation: 78800
There are two problems with your code:
animal[1:]
is the following list
>>> animal[1:]
[[2, 'Eagle', 'Sparrow'], [3, 'Hippo', 'Platypus', 'Deer']]
what do you expect to happen if you join
it in every iteration of your loop?
The second problem is that you do not assign the return value of join
to anything, so even if the operation would not throw an error, you would lose the result.
Here's a solution with extended iterable unpacking:
>>> animal = [[1, 'Crocodile','Lion'],[2, 'Eagle','Sparrow'],[3,'Hippo','Platypus','Deer']]
>>> [[head, ', '.join(tail)] for head, *tail in animal]
[[1, 'Crocodile, Lion'], [2, 'Eagle, Sparrow'], [3, 'Hippo, Platypus, Deer']]
Here's one without:
>>> [[sub[0], ', '.join(sub[1:])] for sub in animal]
[[1, 'Crocodile, Lion'], [2, 'Eagle, Sparrow'], [3, 'Hippo, Platypus, Deer']]
Upvotes: 0
Reputation: 18136
>>> [[a[0], ','.join(a[1:])] for a in animal]
>>> [[1, 'Crocodile,Lion'], [2, 'Eagle,Sparrow'], [3, 'Hippo,Platypus,Deer']]
Upvotes: 1
Reputation: 54313
animal
could be called table
, and should indicate that it's not just a single animal. It also shouldn't be called animals
, because it's not just a list of animals.
Thanks to unpacking, you can split your sub-lists directly into an integer and a list of animals:
>>> table = [[1, 'Crocodile','Lion'],[2, 'Eagle','Sparrow'],[3, 'Hippo','Platypus','Deer']]
>>> [[i, ', '.join(animals)] for (i, *animals) in table]
[[1, 'Crocodile, Lion'], [2, 'Eagle, Sparrow'], [3, 'Hippo, Platypus, Deer']]
Upvotes: 1