Solved

Merge lists using index in Python

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now