0

Sorry, im new in python (or programming in general) & just starting to study for loop. The thing is, i want to combine 2 string lists and register it into for loop to kind of print it in seperate ways, its confusing, but heres the code :

list1 = ['george','james','manon','danielle','chris']
list2 = ['2000','2001','1998','1999','2002']
comblist = list1+list2
for x in comblist:
   print("Hi my name is",x[:5],"and i was born in the year",x[5:10])

I was expecting the output to be like this :

Hi my name is george and i was born in the year 2000
Hi my name is james and i was born in the year 2001
Hi my name is manon and i was born in the year 1998
Hi my name is danielle and i was born in the year 1999
Hi my name is chris and i was born in the year 2002

But instead it turned out like this :

Hi my name is georg and i was born in the year e
Hi my name is james and i was born in the year
Hi my name is manon and i was born in the year
Hi my name is danie and i was born in the year lle
Hi my name is chris and i was born in the year 
Hi my name is 2000 and i was born in the year
Hi my name is 2001 and i was born in the year
Hi my name is 1998 and i was born in the year
Hi my name is 1999 and i was born in the year
Hi my name is 2002 and i was born in the year

What did i do wrong? All help is appreciated. Thanks!

Razani
  • 1
  • Try printing out the ```x``` or the ```comblist```, you will see that the list you are appending is appended at the back – andondraif Oct 12 '20 at 04:17
  • It seems like we've had a few questions recently for a very similar problem. – Karl Knechtel Oct 12 '20 at 04:22
  • "What did i do wrong? " First off, you should check what `comblist` contains, and what `x` contains. And then you should try to explain, in plain English words, how you intend for your logic to work. For example, you should come up with a value that `x` could have each time through the list, that would be useful for solving the problem. *Then* you can work on how to get that value. – Karl Knechtel Oct 12 '20 at 04:23

1 Answers1

0

To do your job:

Method 1: use zip()

for name, year in zip(list1, list2):
     print("Hi my name is",name,"and i was born in the year",year)

Method 2:

for i in range(len(list1)):
     print("Hi my name is",list1[i],"and i was born in the year",list2[i])

You can search more about that code on google search

Programmer dude
  • 167
  • 1
  • 5
  • 23
  • Whenever you see `range(len(some_sequence))`, 99% of the time it can be replaced with `enumerate(some_sequence)` – pepoluan Oct 12 '20 at 04:38
  • @pepoluan, so you decided to write this under answer which belongs to those *1%*, where you shouldn't use `enumerate()`? – Olvin Roght Oct 12 '20 at 04:47
  • @OlvinRoght Why can it not be rewritten using `enumerate`? Very easy to do that: `for i, name in enumerate(list1):\n print(f"Hi my name is {name} and I was born in {list2[i]}")` – pepoluan Oct 12 '20 at 05:01
  • @OlvinRoght where was it wrong, I challenge you to show me. And it's not useless; it's more efficient: Rather than creating a generator + iterating through a generator + doing 2 indexing, using enumerate() results in 1 generator + iterating through the generator + doing 1 indexing. – pepoluan Oct 12 '20 at 05:25