Python Forum

Full Version: Creating list of lists, with objects from lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have two lists like this:
List1 = ['1','2','3','5']
list2 = ['a','b','c','d','e']
and im trying to combine them into one list of lists like this:
Wanted_result = [['1','a'],['2','b'],['3','c'],['4','d'],['5','e']]
I'm able to make one like this:
newlist = [list1[0],list2[0]]
print(newlist)
['1', 'a']
However, I have no idea how to get it working using a forloop. What would be the best way of doing this?

I hope this was understandable, "slight" newbie here Big Grin

Thanks,
Sander
What do you mean by working with forloop? Adding or displaying?
You can use zip. Zip is one of my favorite Python functions. I use it all the time.

https://docs.python.org/3/library/functions.html#zip

In the code below I use zip and a list comprehension to make a list of lists.
numbers = ["1", "2", "3", "5"]
letters = ["a", "b", "c", "d", "e"]

pairs = [[letter, number] for letter, number in zip(letters, numbers)]
print(pairs)
Output:
[['a', '1'], ['b', '2'], ['c', '3'], ['d', '5']]
if you don't understand how a list comprehension works, this is the list code written using a for loop.
pairs = []
for letter, number in zip(letters, numbers):
    pairs.append([letter, number])
print(pairs)
Output:
[['a', '1'], ['b', '2'], ['c', '3'], ['d', '5']]
You can also use the built-in list() function.
pairs = list(zip(letters, numbers))
Output:
[('a', '1'), ('b', '2'), ('c', '3'), ('d', '5')]
Notice that list() makes tuples instead of lists. A tuple is like a list, but you can't modify the contents of a tuple. However, tuples have an advantage in speed and memory usage.

And finally I pair up letters and numbers in a dictionary. You may have a good reason for making lists of lists (or tuples), but you should consider using a dictionary.
pairs = dict(zip(letters, numbers))
Output:
{'a': '1', 'b': '2', 'c': '3', 'd': '5'}
what does your 3-line code give you if you take out both [0]?
(Sep-30-2022, 09:19 PM)deanhystad Wrote: [ -> ]Notice that list() makes tuples instead of lists.

list does not make tuple's, zip is returning the passed in iterables as an iterator of tuple's, by using list on the result of zip it creates a list of those tuple's.
Notice that using list gives you a list of tuples because zip produces tuples.
(Sep-30-2022, 09:19 PM)deanhystad Wrote: [ -> ]You can use zip. Zip is one of my favorite Python functions. I use it all the time.

https://docs.python.org/3/library/functions.html#zip

In the code below I use zip and a list comprehension to make a list of lists.
numbers = ["1", "2", "3", "5"]
letters = ["a", "b", "c", "d", "e"]

pairs = [[letter, number] for letter, number in zip(letters, numbers)]
print(pairs)
Output:
[['a', '1'], ['b', '2'], ['c', '3'], ['d', '5']]
if you don't understand how a list comprehension works, this is the list code written using a for loop.
pairs = []
for letter, number in zip(letters, numbers):
    pairs.append([letter, number])
print(pairs)
Output:
[['a', '1'], ['b', '2'], ['c', '3'], ['d', '5']]
You can also use the built-in list() function.
pairs = list(zip(letters, numbers))
Output:
[('a', '1'), ('b', '2'), ('c', '3'), ('d', '5')]
Notice that list() makes tuples instead of lists. A tuple is like a list, but you can't modify the contents of a tuple. However, tuples have an advantage in speed and memory usage.

And finally I pair up letters and numbers in a dictionary. You may have a good reason for making lists of lists (or tuples), but you should consider using a dictionary.
pairs = dict(zip(letters, numbers))
Output:
{'a': '1', 'b': '2', 'c': '3', 'd': '5'}

Thank you so much!

Wasn't familiar with Zip, will definetly be using this a ton.

Tried all of the methods, but this one turned out to be best for my application:
pairs = []
for letter, number in zip(letters, numbers):
    pairs.append([letter, number])
print(pairs)
Also didn't know that tuples was more efficient, i work with large CSV files so efficiency is great!

Thanks again Smile
lists are less efficient because they need to include the means for any type of object or reference at any position to be changed to any other type of object or reference. if you need to modify your sequence objects, lists can do so directly in-place. to do that as a tuple, you need to build a whole new one. in some use cases, doing the latter could be more efficient. in most use cases, doing the former would be more efficient. it depends on what kind of change is happening.