Solved

Merge lists using index in Python

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

867 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

21 Experts available now in Live!

Get 1:1 Help Now