Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Merge lists using index in Python

Posted on 2016-07-15
1
Medium Priority
?
222 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 17

Accepted Solution

by:
gelonida earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 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…
Suggested Courses

885 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