+ 1

How to add an item from a list to an list which is an item of another list?

For ex: Input X = [ [1,2,34,5] , [1,2,3,4,5] ] Y = [ 2, 3 ] Output should be like: A = [ [1,2,3,4,5,2] , [1,2,3,4,5,3] ]

1st Aug 2019, 10:54 AM
Rudra Tiwari
Rudra Tiwari - avatar
10 RĂ©ponses
+ 3
I’m not writing it for you, its unfair to expect other people to do your work.
1st Aug 2019, 11:18 AM
Rincewind
Rincewind - avatar
+ 2
Take a look at this tutorial on appending to lists in Python: https://www.geeksforgeeks.org/append-extend-python/
1st Aug 2019, 11:00 AM
Rincewind
Rincewind - avatar
+ 2
Rincewind is right, you need append method to do it. The simplest way is to iterate over the two lists, maybe by using zip function, that makes the task easier. You have all the keys to do it, now. We can't show you the code, because you have to do it by yourself to progress.
1st Aug 2019, 11:46 AM
Théophile
Théophile - avatar
+ 2
Append is how you add an item on to a list, it’s up to you to figure out the logic. I know how I would do it but that doesn’t mean I will hand you an answer. Please try it yourself because that’s how everyone who is learning how to code learns.
1st Aug 2019, 11:47 AM
Rincewind
Rincewind - avatar
+ 1
https://code.sololearn.com/cRM6Z9LMYFwU/?ref=app Not the exact approach you gave me, but this works too😎
1st Aug 2019, 2:58 PM
Rudra Tiwari
Rudra Tiwari - avatar
+ 1
That's a good attempt, as long as you understand you have converted a list of ints to a string. If you wanted to keep them as ints, you would have to figure out a way to convert them from lists to ints. A simpler way is index slicing: a[0].append(x[0]) a[1].append(x[1]) I hope you understand that we were not refusing to write the code before your attempt because we're horrible people, but because writing it yourself it the best way to learn; you must understand what you're writing if you want to progress.
2nd Aug 2019, 8:24 AM
Rincewind
Rincewind - avatar
0
Well, I got it thanks
1st Aug 2019, 2:53 PM
Rudra Tiwari
Rudra Tiwari - avatar
0
ThĂ©ophile has literally given the approach you said you were looking for and i’ve told you what method to use - iterate over the lists, and use the append method - all you have to do is follow what we have said and write the code. You can just append each item from list Y onto the correct part of list X if you index it properly, but I’m not writing it out for you. Try your own method and learn.
1st Aug 2019, 2:53 PM
Rincewind
Rincewind - avatar
0
This is great, now its of only 2 lines https://code.sololearn.com/cRM6Z9LMYFwU/?ref=app
2nd Aug 2019, 2:26 PM
Rudra Tiwari
Rudra Tiwari - avatar
- 2
Can you please code the solution? Because append and extend are definitely not helping me😅 Rincewind
1st Aug 2019, 11:08 AM
Rudra Tiwari
Rudra Tiwari - avatar