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
  create a list of object with a list of character studenthch 0 61 Feb-12-2020, 08:43 AM
Last Post: studenthch
  how to compare a list to a list of lists kevthew 1 143 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 604 Dec-16-2019, 12:57 PM
Last Post: BamBi25
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 138 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  item = index position - list of list RavCOder 9 281 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  Dictionary and tuples list comprehensions help paul41 2 225 Nov-29-2019, 06:59 PM
Last Post: perfringo
  more list help converting paul41 3 182 Nov-25-2019, 07:59 AM
Last Post: perfringo
  List Comprehension - Creating a list of the length of an item help paul41 2 228 Nov-18-2019, 10:21 AM
Last Post: perfringo
  Converting parts of a list to int for sorting menator01 2 191 Nov-03-2019, 03:00 PM
Last Post: menator01
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 271 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala

Forum Jump:


Users browsing this thread: 1 Guest(s)