Posts: 2
Threads: 1
Joined: Sep 2022
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
Thanks,
Sander
Posts: 1,144
Threads: 114
Joined: Sep 2019
Sep-30-2022, 08:40 PM
(This post was last modified: Sep-30-2022, 08:40 PM by menator01.)
What do you mean by working with forloop? Adding or displaying?
Posts: 6,780
Threads: 20
Joined: Feb 2020
Sep-30-2022, 09:19 PM
(This post was last modified: Sep-30-2022, 09:22 PM by deanhystad.)
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'}
sgrinderud and Skaperen like this post
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
Sep-30-2022, 11:13 PM
(This post was last modified: Sep-30-2022, 11:13 PM by Yoriz.)
(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.
Posts: 6,780
Threads: 20
Joined: Feb 2020
Oct-01-2022, 01:56 AM
(This post was last modified: Oct-03-2022, 03:50 AM by deanhystad.)
Notice that using list gives you a list of tuples because zip produces tuples.
Posts: 2
Threads: 1
Joined: Sep 2022
(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
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
|