Solved

Merge lists using index in Python

Posted on 2016-07-15
1
152 Views
Last Modified: 2016-08-11
Hi all!

I am trying to merge 3 lists, without success.

titles = ['Mario','Luigi']
prices = ['10','1']
descriptions = ['Mario is cool!','Luigi is boring :(']
game_list = []

for idx, val in enumerate(titles):
    game_list.append([titles[idx], prices[idx], descriptions[idx]])

Open in new window


What am I doing wrong?
0
Comment
Question by:ltpitt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
gelonida earned 500 total points
ID: 41713418
Don't see at the first glance what's wrong. Will copy your code and try it. Next time you should provide the exact error message or the exact behavior. It makes it easier to locate the problem.

Python has a built in function to do exactly this:
game_list = zip(titles, prices, descriptions)

Open in new window



Modification:
I tried your code and it seems to combine the three lists into one list containing  sublists.

titles = ['Mario','Luigi']
prices = ['10','1']
descriptions = ['Mario is cool!','Luigi is boring :(']
game_list = []

for idx, val in enumerate(titles):
    game_list.append([titles[idx], prices[idx], descriptions[idx]])

print(game_list)

Open in new window


The output is:
[['Mario', '10', 'Mario is cool!'], ['Luigi', '1', 'Luigi is boring :(']]

Open in new window


Is this not what you want?

Please show us the expected reslut If you want to combine the three lists differently.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question