Python Forum
Creating list of lists, with objects from lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating list of lists, with objects from lists
#1
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
Reply
#2
What do you mean by working with forloop? Adding or displaying?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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'}
Skaperen and sgrinderud like this post
Reply
#4
what does your 3-line code give you if you take out both [0]?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(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.
Reply
#6
Notice that using list gives you a list of tuples because zip produces tuples.
Reply
#7
(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
Reply
#8
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 347 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 696 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,567 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 772 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 775 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 822 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,445 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 750 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,287 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 782 Feb-28-2023, 10:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020