Solved

Merge lists using index in Python

Posted on 2016-07-15
1
108 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 16

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

735 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