Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Converting List into list of tuples
#1
Here I am trying to convert a List into a dictionary. So, I am following this approach.
my_list = ['a', 'A', 'b', 'B', 'c', 'C']

If I convert a list into a multiple tuples (('a', 'A'), ('b', 'B'), ('c', 'C')), then I can easily convert into a dictionary using dict() built in function.

Please guide me to convert a list into multiples of tuples.
Quote
#2
Look into zip. I'd create an iterator from your list and zip it with itself, but if that sounds too advanced for you, consider slicing's third parameter to slice your list a couple of times into appropriate input for zip.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#3
If you go with zip(), you'll want to slice the list up into lowercase an uppercase characters to get the desired result.

my_list = ['a', 'A', 'b', 'B', 'c', 'C']
for pair in zip(my_list[::2], my_list[1::2]):
    print(pair)
Quote
#4
my_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E']
my_dict = dict([*zip(my_list[::2], my_list[1::2])])
print(my_dict)
Output:
{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E'}
newbieAuggie2019 likes this post
A.D.Tejpal
Quote
#5
One can use zip() with iterator as well:

my_list = ['a', 'A', 'b', 'B', 'c', 'C']
dict(zip(*[iter(my_list)]*2))
Python built-in itertools module documentation contains following recipe:

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)
adt and newbieAuggie2019 like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  List Comprehension - Creating a list of the length of an item help paul41 0 54 11 hours ago
Last Post: paul41
  Converting parts of a list to int for sorting menator01 2 115 Nov-03-2019, 03:00 PM
Last Post: menator01
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 147 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 128 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Converting to a list and sort tantony 6 191 Oct-07-2019, 03:30 PM
Last Post: perfringo
  Creating a list from a comprehension using a list rm197 6 144 Sep-30-2019, 07:02 PM
Last Post: rm197
  convert list of dict to dict of array of list with pop up kk230689 1 139 Sep-13-2019, 07:29 AM
Last Post: perfringo
  calculation with list in list gianniskampanakis 13 623 Aug-09-2019, 12:01 PM
Last Post: gianniskampanakis
  removing quotes from a list and keep type list evilcode1 3 224 Aug-03-2019, 11:07 AM
Last Post: perfringo
  Changing elements of a list to match another list rockproper 2 207 Jul-11-2019, 02:13 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)